GraalVM is now joining openjdk (archive), so it’s time to review how to compile java to native code, because it’s so easy!

1. Single File, no dependencies

Assuming you’ve already installed GraalVM, you have access to native-image.

Compiling a single file with only dependencies in the jdk is a simple 2 steps process:

  1. Generate bytecode with javac;

  2. Generate binary with native-image.

2. Example

Let’s try a simple program fitting for the day:

public class Hello {
  public static void main(String[] args) {
    System.out.println("新春快樂");
  }
}

Then, let’s compile it to a static executable:

javac Hello.java
native-image --no-server --static Hello hello

3. Result

This native executable is ready to be launched:

./hello
新春快樂

This was so easy!