Sub topic 9
Creating the cache in one step with JDK 25
This chapter assumes you have already built a cache with the three-step workflow (record, create, run) on JDK 24. You should also be comfortable with the java launcher options, the classpath, and what a training run is.
What changes in JDK 25
The JDK 24 workflow needs two extra java commands before you can use the cache. You must record a configuration file, then create the cache from it. In JDK 25 a single flag, -XX:AOTCacheOutput, does both.
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
# JDK 25 and later: train and build the cache in one command
java -XX:AOTCacheOutput=app.aot -cp app.jar HelloThis command runs your application as the training run. When the application exits, the JVM writes app.aot. You then start the application with the cache exactly as before:
java -XX:AOTCache=app.aot -cp app.jar HelloNote the two different flag names. AOTCacheOutput names the file to write. AOTCache names the file to read.
What the JVM does for you
The one-step flag does not change what the cache contains. It automates the same two phases you ran by hand.
- The launcher starts a JVM in record mode. That JVM runs your application and writes a temporary configuration file.
- When that run ends, the launcher starts a second JVM in create mode. It reads the temporary file and writes the cache.
- The temporary configuration file is removed.
Because the training run is a normal run of your application, everything you learned about choosing a representative run still applies. The JVM records only the classes your run loaded and linked. If the run exits before it reaches the code paths you care about, those classes are missing from the cache.
Keeping the training run and production run compatible
The cache is validated when you use it, as described in the chapter on cache validation. The one-step workflow makes one mistake harder to make. The training run uses the same command line as the command you typed, so the classpath and module options match by construction. You still need to keep them aligned for production. If you train with -cp app.jar and deploy with -cp /opt/app/app.jar, the paths differ and the JVM can reject the cache.
A simple habit is to put the shared options in one place and add only the cache flag.
OPTS="-cp app.jar"
# Build the cache
java -XX:AOTCacheOutput=app.aot $OPTS Hello
# Production start, with the same options
java -XX:AOTCache=app.aot $OPTS HelloMake a bad cache fail loudly
By default the JVM treats the cache as an optimization. If it cannot use the file, it prints a warning at most and runs without it. That is safe, but it hides a slow start in production. Set -XX:AOTMode=on to change this behavior.
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloWith AOTMode=on the JVM exits with an error if the cache is missing or rejected. Use it in deployment and in CI smoke tests. Leave it off during development, where a silent fallback is usually what you want.
| Setting | Cache unusable | Typical use |
|---|---|---|
| Default (`AOTMode=auto`) | Runs without the cache | Development |
| `AOTMode=on` | JVM refuses to start | Production and CI checks |
When to still use three steps
The two-command form is still available and is sometimes the better choice. You may want to record on one machine and create on another. You may also want to keep the configuration file to inspect it or reuse it. For a simple application whose training run is just a normal start, the one-step flag is shorter and has fewer files to manage.
Exercise
You run java -XX:AOTCacheOutput=app.aot -cp app.jar Server, but Server is killed with SIGKILL after the load test. No app.aot appears. Why, and how would you fix the training setup?
Show answer
The cache is created only after the training run ends normally, and a killed JVM never reaches the create phase. Fix it by making the application shut down cleanly at the end of training, for example by having the test client call a shutdown endpoint, or by running a bounded workload that returns from main. Then deploy with -XX:AOTMode=on -XX:AOTCache=app.aot so a missing or rejected cache fails loudly.