DEV Community

loading...

Quick start to GraalVM

tios profile image Tito ใƒป1 min read

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

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!");
  }
}

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

gu install native-image

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!

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

Discussion (0)

pic
Editor guide