Sub topic 3
The closed-world assumption and what gets included
This chapter assumes you can build a class or JAR with native-image (see the previous chapter) and know what the classpath is. It explains how Native Image decides which code ends up in the executable, and why that decision causes the most common surprises.
What "closed world" means
A JVM can load any class at any time, so it must keep every class available. Native Image works the other way. At build time it assumes that all code the program will ever run is known when the build starts. This is the closed-world assumption.
Under that assumption, the builder can run a static analysis. It starts from the entry points, usually main, and follows every call, field access and type use it can find. Whatever it reaches is compiled into the image. Everything else is dropped.
What the analysis follows
The analysis is a points-to analysis. It tracks which concrete types can flow into each variable, field and call site. That precision matters for virtual calls. If a call is made on an interface, only the implementations that can actually be instantiated and reach that call are kept.
The analysis covers more than your own code. It includes the JDK classes your code uses, plus all libraries on the classpath. Unused JDK parts are removed too, which is one reason images are small compared with a full JVM installation.
A small example
public class Closed {
interface Greeter { String greet(); }
static class English implements Greeter {
public String greet() { return "Hello"; }
}
static class French implements Greeter {
public String greet() { return "Bonjour"; }
}
public static void main(String[] args) {
Greeter g = new English();
System.out.println(g.greet());
}
}French is never instantiated. The analysis sees that only English can reach g.greet(). The call can be compiled as a direct call, and French is left out of the image.
What the analysis cannot see
Static analysis can only follow what appears in the code. These patterns hide a target from it:
- Reflection with names built at run time, such as
Class.forName(name). - Resources loaded by path, such as
getResourceAsStream("/config.properties"). - Dynamic proxies and serialization.
- JNI calls into native libraries.
If the analysis cannot prove a class or method is needed, it removes it. The program then fails when it runs, not when it builds. The next chapters cover how to declare such uses so the builder keeps them.
Seeing what was included
The builder can write a report that shows the reachable methods. Pass the -H:+PrintAnalysisCallTree option or the reports option shown below, and compare the output with what you expected.
native-image -H:+UnlockExperimentalVMOptions -H:+PrintAnalysisCallTree ClosedOptions of this kind change between GraalVM releases, so check the output of native-image --expert-options for your version. The build also prints a summary of reachable classes, fields and methods, which is a quick way to see how much of the JDK was pulled in.
Consequences to plan for
- Smaller images. Dead code is not shipped.
- Fixed behavior. You cannot add a class to the classpath after the build and expect it to load.
- Build-time cost. The analysis reads the whole reachable program, which is why builds are slow and memory hungry.
- Failures at run time. A missing class shows up as an error such as
ClassNotFoundExceptionorNoSuchMethodException, even though the same program ran on the JVM.
Common mistakes
- Assuming a class on the classpath is automatically in the image. Only reachable code is included.
- Adding a plugin JAR to the classpath at launch. The image ignores it.
- Treating a build success as proof of correct behavior. The analysis cannot warn about dynamic uses it cannot see.
Exercise
In the Closed example, why is French left out of the image, and what would you need to change so that it is kept?
Show answer
Nothing in the reachable code instantiates French, so the static analysis never marks it as needed. It is kept if reachable code creates an instance, for example new French() on a path from main, or if it is registered through reachability metadata for reflective access.
Exercise
A program calls Class.forName(System.getenv("PLUGIN")) and works on the JVM. Why might it fail in a native image?
Show answer
The class name is only known at run time, so the closed-world analysis cannot see the target and removes the class from the image. The call then fails at run time unless the class is registered through reachability metadata.