Sub topic 2
The closed-world assumption
This chapter assumes you know Spring Boot basics, have built a project with Maven or Gradle, and understand Java reflection and dynamic proxies at a conceptual level. It explains the rule that shapes every other part of native image work.
What the assumption says
A native image build must know every piece of code your application can run, at the time you build it. This is the closed-world assumption. The native-image tool starts at your entry point, follows every call it can find, and keeps only the classes, methods and fields it reaches. Everything else is left out of the executable.
A JVM works in an open world. It can load any class on the classpath while running, even a class whose name was built from a string a minute ago. A native executable has no class loader that can bring in new bytecode, so it cannot do this.
Static analysis and reachability
The build performs a points-to analysis. It starts with the main method and finds which methods can be called, which types can be instantiated, and which fields can be read. It repeats this until nothing new turns up. The result is the set of reachable code. Unreachable code is removed, which is one reason native executables are small.
Where the analysis cannot see
Analysis follows direct calls well. It cannot follow calls whose target is only known at run time. Three common cases are:
- Reflection, such as
Class.forName("com.example.Greeting")orgetDeclaredMethod(...). - Classpath resources loaded by name, such as
getResourceAsStream("templates/greeting.txt"). - Dynamic proxies created with
java.lang.reflect.Proxy, where the interfaces are chosen at run time.
The analysis sees no static path to the target, so the target may be removed. The program builds without errors and then fails when it runs.
public class ReflectionDemo {
public static void main(String[] args) throws Exception {
String name = args.length > 0 ? args[0] : "java.util.ArrayList";
Object list = Class.forName(name).getDeclaredConstructor().newInstance();
System.out.println("Created " + list.getClass().getName());
}
}On the JVM this works for any class name. In a native image, the class name comes from an argument, so the analysis cannot know which class to keep. If the class was not kept, Class.forName throws ClassNotFoundException. The fix is to tell the build about the class in advance, using reachability metadata. A later chapter covers this.
Other consequences
The same assumption has further effects:
- Class loading and class initialization can happen at build time. Static state computed during the build is stored in the executable.
- Bytecode generated at run time, for example by some proxy libraries, is not supported. The classes must be generated before or during the build.
- The classpath is fixed. Adding a JAR at launch has no effect.
Why this matters for Spring
Spring relies heavily on reflection and proxies. It scans for annotations, creates beans by calling constructors, and wraps beans in proxies. This is why Spring has its own build-time step, called AOT processing. It turns much of that dynamic behavior into plain code the analysis can follow, and it produces the metadata for the rest.
Exercise
A class is loaded with Class.forName using a name read from a configuration file at startup. The application works on the JVM but throws ClassNotFoundException in the native executable. Why does this happen, and what is the general fix?
Show answer
The native build uses static analysis and cannot see which class the configuration file will name, so it may leave that class out of the executable. The fix is to supply reachability metadata (for example through a runtime hint in Spring) that registers the class for reflection, so the build keeps it.