Sub topic 5
Declaring reachability metadata
This chapter assumes you can build a native executable with native-image, and that you know the closed-world assumption: the build analyzes only what it can see statically. Here you will learn how to tell the build about code that is reached dynamically.
What reachability metadata is
Reachability metadata is a set of JSON files that lists the classes, methods, fields and resources your program reaches by name at run time. Static analysis cannot see these accesses, so without metadata the build removes the code or leaves it unreachable. The result is a ClassNotFoundException or a NoSuchMethodException when the executable runs, even though the same program works on a JVM.
You place the files under META-INF/native-image/ on the classpath. The native-image tool reads every such directory it finds, including those inside dependency JARs. A library can therefore ship its own metadata and its users get it automatically.
The metadata files
Each kind of dynamic access has its own file.
| File | Covers |
|---|---|
| `reflect-config.json` | Reflection: classes, constructors, methods, fields |
| `resource-config.json` | Files loaded with `getResource` and similar calls |
| `proxy-config.json` | `java.lang.reflect.Proxy` interface lists |
| `serialization-config.json` | Java serialization of listed classes |
| `jni-config.json` | Access from native code through JNI |
Recent GraalVM releases also support a single combined reachability-metadata.json file. The separate files shown here remain the most widely documented form, and your GraalVM version's documentation tells you which one it expects.
A reflection example
This program loads a class by a name that is 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);
}
}The analysis sees a string, not a class reference. Unless Greeter is reachable some other way, it is not in the image. Register it in META-INF/native-image/reflect-config.json:
[
{
"name": "com.example.Greeter",
"allDeclaredConstructors": true,
"allPublicMethods": true
}
]This entry does two things. It keeps Greeter in the image, and it keeps its constructors and public methods available to reflection. Without the constructor flag, getDeclaredConstructor() would fail even though the class is present.
Keep entries narrow
Setting allDeclaredConstructors and allPublicMethods is convenient, but every registered member adds to the image size and to the code the build must compile. Prefer specific entries when you know what you need. For example, list a single method by name and parameter types instead of every method of the class.
Resources
Resources are not included by default. A call such as getResourceAsStream("/config.properties") needs an entry in resource-config.json that names the file or a pattern. If a resource is missing, the call returns null in the image, and the failure often appears later as a NullPointerException, far from the real cause.
Writing metadata by hand versus generating it
You can write these files by hand, as above, when the dynamic accesses are few and you know them. For larger programs, that approach breaks down: it is easy to miss one access, and the miss shows up only when that code path runs. The next chapter shows how the tracing agent records accesses while the program runs, so you do not have to find them all yourself.
Checking your work
After adding an entry, rebuild and run the executable, then exercise the code path that uses it. A passing build proves nothing here, because missing metadata fails at run time and not at build time.
Exercise
A program calls Class.forName("com.example.Plugin") and works on the JVM. The native executable throws ClassNotFoundException. Which file do you edit, where does it go, and what must the entry contain at minimum?
Show answer
Edit reflect-config.json under META-INF/native-image/ on the classpath. The entry needs the class name "com.example.Plugin". If the code also creates an instance, add the constructor setting (for example allDeclaredConstructors set to true) so that getDeclaredConstructor() works.
Exercise
Your native executable calls getResourceAsStream("/config.properties") and receives null, although the file is in src/main/resources. Why does this happen, and how do you fix it?
Show answer
Resources are not included in a native image by default. Add an entry for config.properties to resource-config.json under META-INF/native-image/, then rebuild.