Sub topic 9
Reachability metadata for reflection, resources and proxies
This chapter assumes you know the closed-world assumption from earlier in the tutorial, and that you understand Java reflection and dynamic proxies at a conceptual level. It explains what reachability metadata is, which kinds exist, and where it comes from.
Why metadata is needed
During a native build, GraalVM analyzes your code and keeps only what it can prove is reachable. Reflection, classpath resources and proxies defeat that analysis, because the target is often a string or computed value, such as Class.forName(name). The analyzer cannot see what that string will contain at runtime.
Reachability metadata is the information you give the build to close that gap. It lists the classes, resources and proxy interfaces the program will touch dynamically. Anything not listed is removed or unavailable in the executable.
The three common kinds
| Kind | What it declares | Typical failure when missing |
|---|---|---|
| Reflection | Classes, and which constructors, methods and fields may be accessed | `ClassNotFoundException`, `NoSuchMethodException`, or a missing-registration error |
| Resources | Classpath files such as templates, `.properties` and `.sql` | `getResourceAsStream` returns `null` |
| Proxies | Interface lists for JDK dynamic proxies | An error saying the proxy class was not found at build time |
GraalVM also has metadata for JNI and serialization. Those are less common in Spring applications.
Where metadata lives
The native build reads metadata files from META-INF/native-image/<groupId>/<artifactId>/ on the classpath. Both your code and your dependencies can contribute files there. Older GraalVM versions use one JSON file per kind: reflect-config.json, resource-config.json and proxy-config.json. Newer versions also accept a single reachability-metadata.json.
You rarely write everything yourself:
- Many libraries ship their own metadata inside their jars.
- The GraalVM Reachability Metadata Repository supplies metadata for popular libraries that do not. The Native Build Tools plugins download it automatically.
- Spring AOT generates metadata for your beans, configuration classes and Spring's own infrastructure.
- You supply the remainder, either as JSON files or as Spring runtime hints. The next chapters cover hints and the tracing agent.
Reading the files
A reflection entry names a class and the access it needs:
[
{
"name": "com.example.demo.Greeting",
"allDeclaredConstructors": true,
"allPublicMethods": true
}
]A resource entry uses a pattern. In resource-config.json this is a regular expression, so \\Q...\\E quotes the literal path:
{
"resources": {
"includes": [
{ "pattern": "\\Qtemplates/greeting.txt\\E" }
]
}
}A proxy entry is a list of interfaces, and each inner list describes one proxy class:
[
["com.example.demo.Greeter"]
]The interface order matters. A proxy for [A, B] is not the same as one for [B, A].
Granularity and cost
Register only what you need. allDeclaredMethods on a large class keeps every method and everything those methods reference, which makes the executable bigger. Prefer narrow entries, such as one named method, when you know what the code calls.
Reading a failure
Reflection errors usually name the class that was not registered. Copy that name into a reflection entry, or into a hint, and rebuild. Resource failures are quieter: the lookup returns null, so the error shows up later as a NullPointerException. When a missing file is suspected, check the resource patterns first.
Exercise
Your application loads templates/greeting.txt with getResourceAsStream. It works with java -jar but returns null in the native executable. Which kind of metadata is missing, and what must it contain?
Show answer
Resource metadata is missing. A resource entry must match the path templates/greeting.txt, for example a pattern in resource-config.json. Without a matching pattern the file is not included in the executable.