Quick start to GraalVM

tios

Theo

Posted on October 3, 2019

Quick start to GraalVM

This post is a TLDR; of Getting started with GraalVM

Finally Java can go real native without the bloat of commerical "native" compilers like ExcelsiorJet or Launchers like Launch4J. Meet GraalVM. With native-image you can AOT (Ahead of time) compile your java code to native executable. When I mean native executable, I mean real native executable unlike Launch4j which embeds a jre or prompts you to get one.

Here is a quick HelloWorld to get started.
Since it is a quick HelloWorld, we dont want to rock the boat by messing up our existing JDK installation. So we will use docker in Windows.

docker run -it -v C:/graalexp:/home/graalexp oracle/graalvm-ce:19.2.0.1 bash
Enter fullscreen mode Exit fullscreen mode

C:/graalexp - is your windows mount. [ Remember to share your C drive with docker in settings]

With a notepad write a HelloWorld.java in C:/graalexp

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, Graal!");
  }
}
Enter fullscreen mode Exit fullscreen mode

Install native-image in the docker container as its not installed by default in graalvm.

gu install native-image
Enter fullscreen mode Exit fullscreen mode

Now compile it to native image. The native image will run on linux targets. If you want native executables to run on windows, its a bit different than the steps listed here.

javac HelloWorld.java
native-image HelloWorld
./helloworld
Hello, Graal!
Enter fullscreen mode Exit fullscreen mode

Thats it.
Note: Graal has commerical as well as community editions. We are using community edition here.

💖 💪 🙅 🚩
tios
Theo

Posted on October 3, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related