Quick revision
The short answer
The class name only exists at run time, in a configuration property, so the native image build could not see it and left the class out; Class.forName then has nothing to find. We fix it by telling the build about the class: register it with Spring's RuntimeHints (a RuntimeHintsRegistrar, or annotations such as @RegisterReflectionForBinding for data types), or add it to the reachability metadata files. The tracing agent can collect missing metadata from JVM test runs, native tests in CI catch the gap before release, and replacing the dynamic name with an explicit registry removes the problem entirely.
What to remember
In this answer, we saw that a class named only in a configuration property is invisible to native image analysis, so the build leaves it out and Class.forName fails at run time. We fixed it by registering the class with Spring's RuntimeHints, or with reachability metadata files, used the tracing agent to find other gaps, and prevented recurrence with native tests in CI and, best of all, with an explicit registry instead of dynamic class names.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The class name only exists at run time, in a configuration property, so the native image build could not see it and left the class out; Class.forName then has nothing to find. We fix it by telling the build about the class: register it with Spring's RuntimeHints (a RuntimeHintsRegistrar, or annotations such as @RegisterReflectionForBinding for data types), or add it to the reachability metadata files. The tracing agent can collect missing metadata from JVM test runs, native tests in CI catch the gap before release, and replacing the dynamic name with an explicit registry removes the problem entirely.
Why this question is asked
This failure surprises teams because everything looks right: the JVM tests pass, the native build finishes without a warning, and the executable starts. Then the first request that needs the configured class fails. Interviewers like it because the fix is easy to copy from a blog, but the reason teaches the closed-world model that every native image problem comes from. In this answer, we will reproduce the failure, see why the build left the class out, go through the fixes with RuntimeHints and metadata files, use the tracing agent to find gaps, and finish with how to stop such gaps reaching production.
1Reproducing the Failure
Suppose a reporting service lets operators choose an exporter class through configuration. The JVM version works for any class on the class path.
Notice that nothing in the code refers to CsvExporter directly. Its name is a string in a YAML file, and the code only knows it through a variable.
2Why the Build Left the Class Out
A native image is built under a closed-world assumption: the build analyses the program from its entry points and includes only the classes, methods and fields it can prove are reachable. Code that is not found is not compiled into the executable, and there is no class loader at run time that could fetch it from a jar later.
The analysis is good at following ordinary code. It can even resolve Class.forName("com.shop.export.CsvExporter") when the name is a constant in the source, because the string is known at build time. But here the name comes from a property that is read when the program runs. No analysis can know it in advance, so from the build's point of view CsvExporter is unreachable, and it is dropped. The constructor call by reflection has the same problem: even if the class were included, its constructor must be registered for reflection to be callable.
- Build starts analysis from main and Spring's AOT code
- Is com.shop.export.CsvExporter referenced in code or metadata?YesClass and constructor includedNoClass left out
- Run time: property gives the class name
com.shop.export.CsvExporter - Class.forName(name)
ClassNotFoundExceptionwhen it was left out
4 steps in order. Play them, or step through them one at a time.
3Fix 1: Register the Class With RuntimeHints
Spring Boot 3 runs AOT processing before the native build and turns RuntimeHints into reachability metadata for us. The most direct fix is a registrar that registers the class and its constructor for reflection.
Notice that the registrar can register every exporter we ship, so any valid configuration works. For types that are only read and written by data binding, such as DTOs that Jackson serialises, the annotation @RegisterReflectionForBinding on a bean or configuration class registers the type with the members binding needs. The member category names have evolved across Spring versions, so check your version's MemberCategory values.
4Fix 2: Add Reachability Metadata Files
Without Spring, or for a library jar that should work in any native build, we can ship the metadata as JSON inside the jar, under META-INF/native-image. The build picks these files up automatically.
Newer GraalVM releases also accept a single reachability-metadata.json file that holds reflection, resources and other entries together, so check your version for the preferred format. Either way, the effect is the same as the hint: the class and its no-argument constructor are included and available to reflection.
5Finding Gaps With the Tracing Agent
Writing metadata by hand works for one class; for a large application we want help. GraalVM's tracing agent runs with the application on a normal JVM, watches every reflective call, resource lookup and proxy creation, and writes the metadata it observed.
There is a catch: the agent only records what actually ran. If the tests configured CsvExporter but production may configure PdfExporter, the agent will not know about PdfExporter. So we review the generated files, keep only what we understand, and still add hints for every valid configuration value.
6Stopping Such Gaps Before Release
The most important lesson is that JVM tests do not prove the native executable works. Spring Boot's build plugins can compile the test suite into a native executable and run it, for example with ./mvnw -PnativeTest test with the Spring Boot parent, or ./gradlew nativeTest. Running native tests in CI, at least nightly and before every release, catches missing hints the same day they are introduced.
We can also remove the dynamic name. A configuration value that selects from a known set does not need to be a class name at all. An explicit registry, a map from a short key such as csv or pdf to a factory, or a set of Spring beans looked up by name, is visible to the analysis with no hints, gives a clear error for an unknown key, and stops configuration from loading arbitrary classes, which is a security improvement as well.
7Follow-up questions
Why did the build succeed if the class was missing?
Because the build only includes what it can see and has no way to know a property will name another class at run time. From its point of view nothing is missing. Some newer GraalVM versions offer stricter modes that report reflective calls the build could not resolve, so check your version.
When should we use @RegisterReflectionForBinding instead of a registrar?
When the type is a data class that is serialised or bound, such as a DTO handled by Jackson. The annotation registers the type and the members binding needs. A registrar is the general tool, for anything else, such as classes created by name, resources or proxies.
Does adding hints for many classes hurt the native image?
A little. Each registered class and member stays in the executable and adds to its size and build time. For a handful of exporters this is negligible; registering whole packages by default would undo part of the benefit, so we register what we need.
8Conclusion
In this answer, we saw that a class named only in a configuration property is invisible to native image analysis, so the build leaves it out and Class.forName fails at run time. We fixed it by registering the class with Spring's RuntimeHints, or with reachability metadata files, used the tracing agent to find other gaps, and prevented recurrence with native tests in CI and, best of all, with an explicit registry instead of dynamic class names.
Open with
The native build works under a closed-world assumption: it includes only the classes it can prove are reachable from the entry points. Here the class name comes from a configuration property, so it is only known at run time, and the build had no way to see that CsvExporter is used.
Then, if they want more
It was left out, and Class.forName throws ClassNotFoundException. The JVM tests pass because on the JVM any class on the class path can be loaded. To fix it in Spring Boot 3, I would add a RuntimeHintsRegistrar that registers the class and its constructor for reflection, imported with @ImportRuntimeHints, and I would unit-test that registrar with RuntimeHintsPredicates.
For data classes used by Jackson, @RegisterReflectionForBinding is the shortcut. Without Spring, the same thing goes into reachability metadata JSON under META-INF/native-image. To find other gaps, I can run the tests on the JVM with the native image tracing agent, which records reflection and resources it observes, but I would review its output because it only sees what ran.
Then I would add native tests to CI so the next missing hint is caught before release, and I would consider replacing the class name with a key into an explicit registry, which the build can see and which also stops configuration from loading arbitrary classes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot 3 native image builds successfully, but at run time it fails with...": https://scrivio-interview-library.netlify.app/java/startup-and-footprint/b75d031590/