Sub topic 12
Diagnosing common failures
This chapter assumes you can build a native executable with native-image, and that you know what reachability metadata and the tracing agent do. Native Image failures fall into two groups: the build stops, or the build succeeds and the program misbehaves. Each group has a short list of usual causes.
Decide where the failure happens
Build-time failures
Read the first error in the output, not the last. Later messages are often consequences of it.
Initialization errors
A common message says an object of some type was found in the image heap, or that a started thread or a random seed was captured at build time. The cause is a class that ran its static initializer during the build, so its state was saved into the binary. Typical offenders are classes that open sockets, read the clock, or create Random instances.
To find out which class initialized when, ask the builder to trace it:
native-image --trace-class-initialization=com.example.Config -jar app.jar -o appIf the class must not run at build time, defer it:
native-image --initialize-at-run-time=com.example.Config -jar app.jar -o appDeferring one class can expose another, so repeat until the build passes.
Unsupported or missing classes
Errors about a missing class usually mean the classpath given to native-image is incomplete. Check that every dependency is on the classpath or inside the JAR. Add -H:+ReportExceptionStackTraces to see full stack traces for errors raised inside the builder.
Builder runs out of memory
The build is a large analysis and needs far more memory than a normal compile. Pass a larger heap to the builder process with the -J prefix:
native-image -J-Xmx8g -jar app.jar -o appDuring development, -Ob gives a faster, less optimized build.
Run-time failures
If the build succeeds but the program fails, the cause is almost always metadata that static analysis could not see.
Reflection
This program loads a class by a name only known at run time:
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);
}
}In the image it can fail with ClassNotFoundException, NoSuchMethodException, or a missing-registration error, depending on what is absent and on the version. The fix is an entry in reflect-config.json under META-INF/native-image/:
[
{
"name": "com.example.Greeter",
"allDeclaredConstructors": true,
"allPublicMethods": true
}
]Resources
A missing resource is quieter. getResourceAsStream("config.properties") returns null because the file was not packed into the image, so the failure shows up later as a NullPointerException. Register the file in resource-config.json:
{
"resources": {
"includes": [
{ "pattern": "\\Qconfig.properties\\E" }
]
}
}Let the agent find the gaps
When you do not know what is missing, run the application on a GraalVM JVM with the tracing agent and exercise every code path:
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image -jar app.jarThe agent only records what it observes. A branch your test run never reached stays unregistered.
Common symptoms
| Symptom | Likely cause | First action |
|---|---|---|
| Build error about an object in the image heap | Class initialized at build time | Trace it, then use `--initialize-at-run-time` |
| Builder killed or out of memory | Heap too small | Add `-J-Xmx8g` or free memory |
| `ClassNotFoundException` at run time | Missing reflection metadata | Add `reflect-config.json` or run the agent |
| `NullPointerException` on a resource | Resource not included | Add `resource-config.json` |
| Works on the JVM, fails natively | Dynamic feature not registered | Compare agent output with your config |
Exercise
Your native executable starts, then throws a NullPointerException when it reads templates/mail.html through getResourceAsStream. The same JAR works on the JVM. What is the most likely cause and the fix?
Show answer
The resource was not included in the image, so getResourceAsStream returns null. Register the file in resource-config.json under META-INF/native-image/, or run the application with the tracing agent so it records the resource, then rebuild.
Exercise
A build fails because a class opened a network connection in its static initializer. Which option defers its initialization, and what do you do if another class then fails the same way?
Show answer
Use --initialize-at-run-time=<class name> for the failing class. Repeat the build and add each further class that fails, using --trace-class-initialization to see which classes are involved.