Sub topic 6
Collecting metadata with the tracing agent
This chapter assumes you can build a native executable with native-image, and that you know why reflection, resources and proxies need reachability metadata. If either point is unclear, review the previous chapters first.
Why use an agent
Writing reflect-config.json by hand works for one class. It does not scale to an application with dozens of reflective calls, resource lookups and dynamic proxies. The tracing agent automates the work. It is a JVM agent that ships with GraalVM. It watches a normal run of your program and records every reflective access, resource load, proxy creation and JNI lookup it sees. It then writes the matching metadata files.
Running the agent
Run the application on a GraalVM JVM, not a native executable, and add the agent option:
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image -jar app.jarWhen the program exits, the output directory holds files such as reflect-config.json, resource-config.json, proxy-config.json, jni-config.json and serialization-config.json. Only the categories your run used are written. The native-image tool reads anything under META-INF/native-image/ on the classpath, so rebuilding the image picks the files up with no extra flags.
Coverage is the main limit
The agent records only what actually happens during the run. A code path that you never execute leaves no metadata, and the native executable fails on that path later. To get good coverage:
- Exercise every feature, including error paths and rarely used endpoints.
- Run your test suite under the agent. Tests often reach code that manual runs miss.
- Do not rely on one run alone, as described below.
For example, take the Reflective program from earlier, which loads com.example.Greeter by name. Run it once under the agent and the output includes an entry for Greeter. If the class name came from a command-line argument and you tested only one value, the other classes you might load are not registered.
Merging several runs
You can run the agent more than once and keep adding to the same directory. Use config-merge-dir instead of config-output-dir:
java -agentlib:native-image-agent=config-merge-dir=src/main/resources/META-INF/native-image -jar app.jarWith config-output-dir, each run replaces the files from the previous one. With config-merge-dir, new entries are merged into the existing files. This lets you build up coverage across several scenarios, such as a normal run, an admin run and a run with an unusual configuration.
Reviewing the output
Treat the generated files as a starting point. Open them and check:
- Noise. The agent also records accesses made by the JDK and by frameworks during startup. Most are harmless, but a very large file slows the build and grows the binary.
- Test-only entries. Running tests under the agent can register test libraries that production code never uses. Remove them.
- Missing entries. Anything your run did not trigger must be added by hand, using the same JSON format as
reflect-config.jsonfrom the previous chapter.
The agent also supports filters, which exclude packages from the output so the files stay smaller. Use them when the noise becomes a problem.
Where the files should live
Placing the output in src/main/resources/META-INF/native-image means Maven and Gradle include the files in the JAR. The native-image tool then finds them automatically. Commit the files to version control, and regenerate them when you add dependencies or reflective code.
Exercise
You ran the tracing agent once with config-output-dir while testing only the login feature. A week later you run it again for the reporting feature, using the same directory and the same option. What happens to the login metadata, and what should you have done?
Show answer
The second run overwrites the files, so the login metadata is lost. You should have used config-merge-dir for the second run, so the reporting entries are added to the existing login entries.