Here’s how you can start using GraalVM for your Scala projects.

Download GraalVM from the website: https://www.graalvm.org/downloads

Unzip the archive; let’s call the resulting directory GRAALVM_HOME

GraalVM distribution you obtained is a Java 8 SE compliant Java Virtual Machine (based on OpenJDK 1.8.0_212).

It means that you can use the java command and run Scala applications as you normally would:

→ $GRAALVM_HOME/bin/java -version

java version “1.8.0_212”

Java(TM) SE Runtime Environment (build 1.8.0_212-b31)

Java HotSpot(TM) GraalVM EE 19.0.0 (build 25.212-b31-jvmci-19-b01, mixed mode)

Or if you have a jar file that you’d like to run, you can run it:

$GRAALVM_HOME/bin/java -jar myJar.jar

Of course, you can also set GraalVM as the default Java on your machine, and add it to the path, for example, I have the following lines in the ~/.bashrc file (on macOS make GRAALVM_HOME point to graalvm/Contents/Home like below, on Linux there’s no Contents/Home )

GRAALVM_VERSION=ee-19.0.0 export GRAALVM_HOME=/Users/shelajev/repo/graalvm/graalvm-releases/graalvm-$GRAALVM_VERSION/Contents/Home export PATH=$GRAALVM_HOME/Contents/Home/bin:$PATH export JAVA_HOME=$GRAALVM_HOME

After that whenever you run java you’ll get the one from the GraalVM distribution.

→ which java

/Users/shelajev/repo/graalvm/graalvm-releases/graalvm-ee-19.0.0/Contents/Home/bin/java → java -version

java version "1.8.0_212"

Java(TM) SE Runtime Environment (build 1.8.0_212-b31)

Java HotSpot(TM) GraalVM EE 19.0.0 (build 25.212-b31-jvmci-19-b01, mixed mode)

Using GraalVM with sbt

Now, if you use sbt for building your project, you can specify which java should be used on the command line with the -java-home argument

Let’s create a sample sbt project you can try it on:

mkdir sbt-example

cd sbt-example

touch build.sbt

mkdirs -p src/main/scala/example

Then in the example directory create the application file Hello.scala :

Start sbt with the -java-home pointing to the GRAALVM_HOME , execute the run command to run our sample code:

→ sbt -java-home $GRAALVM_HOME

[info] Loading project definition from /Users/shelajev/repo/sbt-example/project

[info] Loading settings for project sbt-example from build.sbt ...

[info] Set current project to sbt-example (in build file:/Users/shelajev/repo/sbt-example/)

[info] sbt server started at local:///Users/shelajev/.sbt/1.0/server/f83564437c56d4622007/sock

sbt:sbt-example> run

[info] Packaging /Users/shelajev/repo/sbt-example/target/scala-2.12/sbt-example_2.12-0.1.0-SNAPSHOT.jar ...

[info] Done packaging.

[info] Running example.Hello

/Users/shelajev/repo/graalvm/graalvm-releases/graalvm-ee-19.0.0/Contents/Home/jre

[success] Total time: 1 s, completed Jun 11, 2019 6:40:09 PM

sbt:sbt-example>

You can see that the code is running using the provided Java. So you can use GraalVM without placing it on the PATH.

GraalVM shows great performance results for Scala projects; Twitter is using the GraalVM compiler for their microservices; GraalVM can speed up Scala compilation too (because scalac is a Scala program and runs faster on GraalVM), and we know that GraalVM performs really well on the benchmarks too:

Sample Renaissance benchmark measurements showing performance of multiple JVM implementations

Give it a try, see what performance impact GraalVM shows on your code. If there are any issues or feedback, we’d love to hear about it. The GitHub repository is probably the best place to leave such feedback: oracle/graal; and if you happen to have a success story or any questions, please tell us too: twitter.com/graalvm.