Sub topic 2
Building and running your first native image
This chapter walks through building a native executable from a single class and from a JAR. It assumes you can compile and run Java from the command line, know what a JAR and the classpath are, and have a general idea of what the JVM and the JIT compiler do. You also need a GraalVM distribution with the native-image tool on your PATH, plus a C toolchain for your platform (for example gcc and the zlib headers on Linux).
What the native-image command does
native-image is a compiler driver. It reads compiled bytecode, finds everything the program can reach, compiles that code ahead of time to machine code, and links it with a small runtime called Substrate VM. Substrate VM provides garbage collection, thread support and other services. The result is a standalone executable that does not need a JVM to run.
The build is a two-step process: you compile with javac first, then hand the classes to native-image.
A first example
Start with a minimal program.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, native image!");
}
}Compile it, build the image, and run it.
javac Hello.java
native-image Hello
./helloThe executable is named after the main class, in lowercase, and is written to the current directory. On Windows it gets an .exe suffix. Running ./hello prints Hello, native image! without starting a JVM.
The build takes noticeably longer than javac, often tens of seconds even for this tiny program. Most of that time goes to analyzing the program and the JDK classes it uses.
Building from a JAR
Real applications usually ship as an executable JAR. Pass it with -jar, and name the output with -o.
native-image -jar app.jar -o appnative-image reads the Main-Class entry from the JAR manifest to find the entry point. If your application has dependencies, the manifest's Class-Path entry or a single "fat" JAR must make them available, because the tool needs every class at build time. You can also build from the classpath directly with native-image -cp <classpath> <MainClass>.
Reading the build output
The build prints several stages. The ones worth recognizing are:
| Stage | What it tells you |
|---|---|
| Initializing | Which classes were initialized at build time |
| Performing analysis | How many types, methods and fields are reachable |
| Building universe and compiling methods | Machine code is being generated |
| Creating image | The binary is written, with a size breakdown |
The final summary shows the image size, split into code area, image heap and resources. This is a quick way to see what you are paying for.
Where native images behave differently
A native executable runs the same main method, but its behavior can differ from the JVM in a few ways.
- There is no class loading at run time, so the set of classes is fixed when you build.
- Code that the analysis cannot see as reachable is left out. This is the closed-world assumption, covered in the next chapter.
- Code that finds classes by name at run time, such as the following, fails unless the build is told about the class.
public class Reflective {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.example.Greeter");
Object o = c.getDeclaredConstructor().newInstance();
System.out.println(o);
}
}The build succeeds because the class name is only a string. At run time the executable throws ClassNotFoundException, since Greeter was never included. Later chapters show how to fix this with reachability metadata.
Common first-build problems
native-image: command not found: the GraalVMbindirectory is not on yourPATH. Check withnative-image --version.- A linker error about a missing compiler or library: install the C toolchain for your platform.
- The build is killed or runs out of memory: close other programs or give the builder more memory.
no main manifest attributewith-jar: the JAR has noMain-Classentry. Rebuild it as an executable JAR.
Exercise
You build a program with native-image -jar app.jar -o app. It builds without errors, but at run time it throws ClassNotFoundException for a class that your code loads with Class.forName. Why does the build succeed, and what does the failure tell you?
Show answer
The build analyzes code statically. A class name passed as a string is not visible to that analysis, so the build cannot tell the class is needed and leaves it out. The run-time failure shows the class is missing from the image. It must be registered, for example through reachability metadata.
Exercise
Which command builds a native executable named greet from a compiled class Greeter in the current directory?
Show answer
native-image Greeter -o greet