Getting started with GraalVM for Scala

Oleg Šelajev
Jun 11 · 3 min read

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

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 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 file (on macOS make point to like below, on Linux there’s no )

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

After that whenever you run 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 for building your project, you can specify which should be used on the command line with the argument

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

mkdir sbt-example
cd sbt-example
touch build.sbt
mkdirs -p src/main/scala/example

Then in the directory create the application file :

Start with the pointing to the , execute the 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 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.

graalvm

GraalVM team blog - https://www.graalvm.org

Oleg Šelajev

Written by

Developer advocate for GraalVM at OracleLabs. VirtualJUG organizer. Became Java Champion in 2017. Likes programming languages, compilers, performance, chess

graalvm

graalvm

GraalVM team blog - https://www.graalvm.org