Java / GraalVM Interview questions
How do you troubleshoot "ClassNotFoundException" in a native image at runtime?
This almost always means the class was reachable only through reflection or a dynamic lookup that the build-time static analysis couldn't see, so it was stripped out of the final executable.
- Re-run the application on a normal JVM under the
native-image-agent:java -agentlib:native-image-agent=config-output-dir=META-INF/native-image -jar app.jar, exercising the exact code path that throws the exception. - Check the generated
reflect-config.jsonto confirm the missing class and its constructors/methods got captured. - If the class is loaded by name from a string built at runtime (e.g. a plugin name from a config file), add it manually to the reflection config since the agent can only record paths it actually observes.
- Rebuild with
native-imageand re-test the same code path.
If the class comes from a third-party library, check the GraalVM Reachability Metadata Repository first - there's a good chance someone has already published the correct configuration for that library and version.
More Related questions...