Sub topic 13
Diagnosing native image failures
This chapter assumes you can build a native executable with ./mvnw -Pnative native:compile or ./gradlew nativeCompile. It also assumes you know what reflection and dynamic proxies are, and that you have read the chapters on runtime hints and the tracing agent.
Two kinds of failure
Native failures fall into two groups. They need different tactics.
- Build-time failures.
native-imagestops with an error. Typical causes are a class initialized at the wrong time, a missing dependency, or too little memory for the build. - Runtime failures. The build succeeds, but the executable breaks. The application works on the JVM and fails only in native mode, because the closed-world analysis did not see something your code uses dynamically.
Runtime failures are more common and more confusing, because the JVM run gave no warning.
Reading the symptoms
| Symptom | Likely cause | Fix |
|---|---|---|
| Exception about a missing reflection registration, or `NoSuchMethodException` on a class that exists | Class used by reflection is not in the metadata | Register it with `registerType` |
| `getResourceAsStream` returns `null`, or a file-not-found error for a classpath file | Resource was not included in the executable | Register it with `resources().registerPattern` |
| Error creating a proxy | Proxy interfaces not registered | Register the proxy hint |
| A bean exists on the JVM but not in the executable | A condition was evaluated at build time | Set the property at build time |
| Build is killed or reports out of memory | `native-image` needs a lot of RAM | Give the build more memory |
Start from the exception
The first useful line in the stack trace usually names the class or resource. Take a class that is loaded reflectively, for example by a JSON library, and add a hint for it:
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
@Configuration
@ImportRuntimeHints(AppConfig.GreetingHints.class)
public class AppConfig {
static class GreetingHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(Greeting.class,
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS);
hints.resources().registerPattern("templates/greeting.txt");
}
}
public record Greeting(String text) {}
}If you do not know which hints are missing, run the application on a JVM with the tracing agent and exercise every code path. The agent writes metadata files that native-image reads:
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image -jar target/demo.jarThe agent only records what it sees. A code path you never triggered will not appear in the output.
Conditions fixed at build time
If a bean is missing only in native mode, look at its conditions. Spring evaluates them during AOT processing:
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeatureConfig {
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")
public FeatureService featureService() {
return new FeatureService();
}
public static class FeatureService {}
}If app.feature.enabled is not true when you build, the bean is not in the executable. Setting it when you start the executable does not add the bean. The same applies to profiles, so activate them at build time.
Confirm the fix with native tests
After each change, run the tests as a native image. They find the same failures before production does:
./mvnw -PnativeTest testExercise
Your application runs correctly with java -jar, but the native executable fails at startup because it cannot find templates/greeting.txt on the classpath. What is the cause, and what do you change?
Show answer
The closed-world analysis did not include the resource in the executable, because nothing the analysis could see referenced it. Register it with hints.resources().registerPattern("templates/greeting.txt") in a RuntimeHintsRegistrar imported with @ImportRuntimeHints. Alternatively, run the application on a JVM with the tracing agent while it loads the file. Then rebuild and run the native tests.