Sub topic 9
Creating the cache in one step with JDK 25
The previous chapter built the cache in three commands: record, create, run. JDK 25 lets you collapse the first two into one command. This chapter covers the flag, what the JVM does behind it, when you should still use the three-step form, and how to make a missing or unusable cache fail loudly.
This chapter assumes you can run the three-step workflow on JDK 24 or later. It also assumes you know what a training run is and why the training and production runs must use the same JDK and classpath. You should be able to run and time shell commands.
What changed in JDK 25
On JDK 24 you had to name an intermediate configuration file and invoke the JVM twice before the cache existed:
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jarJDK 25 adds the flag -XX:AOTCacheOutput. You give it the path of the cache file you want, and the JVM handles the rest of the process. JEP 514, "Ahead-of-Time Command-Line Ergonomics", introduced it.
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
# JDK 25 and later: record and create the cache in a single command
java -XX:AOTCacheOutput=app.aot -cp app.jar Hello
# Use the cache
java -XX:AOTCache=app.aot -cp app.jar HelloThe first command runs Hello as a normal training run. It prints ADA, GRACE, LINUS, and when the application exits, a cache file named app.aot appears. You never see or manage the configuration file.
What happens behind the flag
-XX:AOTCacheOutput does the same work as the two JDK 24 steps. It does not introduce a new cache format.
- The JVM starts in record mode and writes the record of loaded and linked classes to a temporary configuration file.
- The application runs to completion. This is your training run.
- When the application exits, the JVM creates the cache from the temporary configuration file. It then deletes that file.
The output is the same kind of cache that the three-step workflow produces, and you use it with -XX:AOTCache.
The cache is created when the training run ends. Two consequences follow:
- The application must exit. A server that never stops will never trigger the create step. Give the training run a way to finish, such as a command-line option that makes the application shut down after it finishes initialization and handles a few test requests.
- Exit takes longer. Creating the cache takes time, from a fraction of a second for
Helloto many seconds for a large application. Do not read the total time of the training command as startup time.
If the process is killed abruptly, for example with kill -9, the JVM gets no chance to create the cache. Treat a missing output file as a failed build step.
The AOT modes
-XX:AOTMode has five values. You rarely need to set it by hand in JDK 25, but knowing what each value does helps when you troubleshoot.
| Value | Meaning |
|---|---|
| `off` | Ignore any AOT cache and run normally. |
| `record` | Run the application and write a configuration file. Use with `-XX:AOTConfiguration`. |
| `create` | Build a cache from a configuration file. Use with `-XX:AOTConfiguration` and `-XX:AOTCache`. |
| `auto` | Use the cache if `-XX:AOTCache` names one and it is usable. Otherwise continue without it. This is the default. |
| `on` | Require the cache. If it is missing or rejected, the JVM exits with an error. |
-XX:AOTCacheOutput implies the record-then-create sequence, so you do not set record or create yourself. The flags -XX:AOTMode=record and -XX:AOTMode=create remain valid and are still the right tool in some situations, covered below.
Failing loudly with AOTMode=on
The default mode is auto, which is forgiving. If the cache file is missing, or was built by a different JDK, or the classpath no longer matches, the JVM runs without the cache. The application still works, only slower. In a development shell this is convenient. In production it can hide a regression: someone updates a JAR, the cache is silently rejected, and startup time doubles with no error to show for it.
Set -XX:AOTMode=on to turn that silent fallback into a failure:
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloIf the JVM can use the cache, the program runs as usual. If it cannot, the JVM prints an error and exits with a non-zero status. A deployment health check or a CI smoke test then catches the problem at once.
A typical test for a build pipeline looks like this:
#!/usr/bin/env bash
set -euo pipefail
JAR=app.jar
CACHE=app.aot
rm -f "$CACHE"
java -XX:AOTCacheOutput="$CACHE" -cp "$JAR" Hello
# The file must exist and be non-empty
test -s "$CACHE"
# The cache must be accepted by the JVM, or this command fails the build
java -XX:AOTMode=on -XX:AOTCache="$CACHE" -cp "$JAR" Hello > /dev/null
echo "AOT cache $CACHE is valid for $JAR"The script deletes any old cache first so a stale file cannot mask a failed training run. set -e stops the script if any step returns a non-zero status. The final command is the real check: with AOTMode=on, a rejected cache fails the build.
Where to use each mode
- Use
autoon developer machines and wherever the cache is only an optimization. - Use
onin CI verification steps and in staging, where you want to learn at once about an invalid cache. - Think carefully before using
onin production. If the cache goes missing,onturns a slower startup into an outage. Prefer to test withonbefore release and runautoin production, with startup time monitored.
Passing training arguments
The training run is a real run of your application. Anything that changes which classes load changes what the cache contains. Pass the same options you use in production, and add the arguments that exercise the typical code path:
java -XX:AOTCacheOutput=app.aot \
-cp app.jar com.example.Server --training --port 0Here --training and --port 0 are examples of options you would add to your own application. --training could tell the application to run a few requests and then exit. --port 0 asks the operating system for any free port, which avoids conflicts on a build machine. The JVM does not understand these flags. They belong to the application.
Keep the JVM options the same between training and production, especially the options that affect class loading and the heap layout. The next chapter on cache validation covers the specific rules. A good habit is to put the shared JVM options in one place and use them in both commands.
When to keep the three-step workflow
The one-step flag does not replace the three-step workflow in every case. Keep using the longer form when:
- You target JDK 24.
-XX:AOTCacheOutputdoes not exist there, and the JVM will refuse to start with an unrecognized-option error. - You want to inspect or reuse the configuration file. The
.aotconffile is a build artifact you can keep. For example, you can record on one machine and create the cache on another, as long as both use the same JDK and classpath. - Training and creation need different settings. The create step reads the configuration and does not run your application. If you want the create step to run in a different pipeline stage, for example a stage with more memory, you need the intermediate file.
- You want several training runs to feed one cache. The explicit form lets you control which configuration goes into the create step.
For a single-machine build with one training run, the one-step form is shorter and has fewer places to go wrong.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Unrecognized VM option 'AOTCacheOutput'` | The JDK is older than 25. | Use JDK 25 or later, or use the three-step workflow. |
| No cache file after the command | The application crashed or was killed before a normal exit. | Make the training run finish by itself, and check its exit status. |
| The command never finishes | The training run is a server that does not stop. | Add a way to stop it after the training work. |
| Cache exists but startup is not faster | The training run did not load the classes used in production, or the production command line differs. | Check the training arguments. Verify with `-Xlog:class+load`. |
| `AOTMode=on` run fails | The cache is missing, or the JDK or classpath differs from training. | Rebuild the cache with the exact production JDK and classpath. |
| Old cache silently reused as "output" | The previous file was left in place after a failed training run. | Delete the output file before each build. |
Checking that the cache works
Building the cache is not enough. Confirm that the JVM uses it, using the same methods you saw earlier:
time java -cp app.jar Hello
time java -XX:AOTCache=app.aot -cp app.jar Hello
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloFor a program as small as Hello, the difference is a few tens of milliseconds, and noise can hide it. Run each command several times and compare the typical values, not a single run. Larger applications that load thousands of classes show much bigger gains.
Summary
-XX:AOTCacheOutput=<file>in JDK 25 and later records a training run and creates the cache when the application exits.- The result is the same cache that the three-step workflow produces, and you use it with
-XX:AOTCache=<file>. - The application must exit normally, so design the training run to stop on its own.
-XX:AOTMode=autois the default and falls back silently.-XX:AOTMode=onmakes an unusable cache a startup error, which suits CI and staging.- Keep the three-step workflow for JDK 24, for reusing the configuration file, or for splitting training and creation across stages.
Exercise
Your CI job runs java -XX:AOTCacheOutput=app.aot -cp app.jar Server and hangs until the job times out. Locally the same command finishes. Name the most likely cause, and give a fix. Then write the command that makes a later pipeline step fail if the cache cannot be used.
Show answer
The cause is most likely that Server is a long-running application that does not exit on its own in CI. The cache is created only when the JVM exits, so the command never reaches the create step. The fix is to give the training run a way to finish, for example an application option such as --training that handles a few requests and then shuts down normally. The verification command is java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar Server. With AOTMode=on, the JVM exits with an error if the cache is missing or rejected, so the step fails. Because that command also starts the application, it needs the same stop mechanism, or you can use a short-lived entry point for the check.
Exercise
A teammate runs the build on JDK 24 and reports that -XX:AOTCacheOutput=app.aot gives an unrecognized-option error. Write the two commands that build the cache on JDK 24 for the class Hello in app.jar, using app.aotconf as the configuration file.
Show answer
First record: java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello. Then create: java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jar. The one-step flag was added in JDK 25, so JDK 24 needs both steps.