Sub topic 8
When the JVM accepts or rejects a cache
This chapter assumes you can build a JAR, run the record, create and run steps from the previous chapter, and read JVM log output. It covers the checks the JVM makes before it uses an AOT cache, what happens when a check fails, and how to make a failure visible.
Why validation exists
An AOT cache holds a snapshot of the JVM's own view of your classes. It stores parsed class data, linking results, and some pre-built heap objects. The snapshot is only correct if the JVM that reads it sees the same inputs as the JVM that wrote it. If a JAR changed between the two runs, the cache could contain a class that no longer matches the file on disk. Using it would run the wrong code.
So the JVM validates the cache when it starts. If any check fails, the JVM does not guess. It stops using the cache, or part of it, and loads classes the normal way. Your program still runs, only slower.
This is the main thing to remember. By default, a rejected cache costs you speed, not correctness, and it usually does not produce an error.
What the JVM checks
Validation happens in layers. A cache must pass the earlier layers before the later ones matter.
1. The file itself
The JVM first checks that the cache file exists, is readable, and has a valid header. A missing path, a truncated file, or a file that is not a cache fails here.
2. The JVM build
The cache is tied to the JVM that created it. The header records the JVM version and build details, the operating system, and the CPU architecture. A cache created with one JDK build is not guaranteed to work with another. This includes a different patch release of the same major version, or a different vendor's build of the same release.
Treat the cache as a build artifact of a specific JDK. Create it with the same JDK installation that will run the application.
3. JVM options that change the data layout
Some options change how objects or class metadata are laid out in memory. Compressed object pointers and compressed class pointers are the common examples. If you create the cache with one setting and run with another, the stored data no longer fits. The JVM rejects the cache or the affected part of it.
The practical rule is to run with the same layout-affecting options you used for the training run. These include heap size settings that change whether compressed pointers can be used. If the heap size crosses the limit for compressed pointers, the JVM can silently switch modes.
Some features also disable parts of the cache. Examples are options that alter classes at load time, such as --patch-module, and agents that rewrite class bytes. If your application uses a Java agent, check the logs rather than assuming the cache is fully used.
4. The class path
The class path is where most real-world rejections come from. The rule is the same as for Application Class Data Sharing:
- The class path used at run time must start with the class path used at training time. The training class path must be a prefix of the run-time class path.
- Each JAR on the training class path must be unchanged. The JVM compares the file's size and modification time.
This has several consequences, listed in the table below.
| Change between training and run | Result |
|---|---|
| Same class path, same JARs | Cache accepted |
| New JAR appended at the end of the class path | Cache accepted for the original entries |
| New JAR inserted at the front or middle | Cache rejected, because the prefix no longer matches |
| Entries reordered | Cache rejected |
| A JAR rebuilt, copied with a new timestamp, or touched | Cache rejected |
| Same JAR moved to a different directory | Depends on how the path is compared. Do not rely on it. |
Appending is allowed because classes in the original entries still resolve first. The JVM can trust the cached data for those entries. A JAR placed earlier could shadow a cached class, so that case is refused.
Classes that come from a directory on the class path are not stored in the cache. A directory has no single size and timestamp the JVM can check. Package your application as JARs if you want it cached.
Classes from custom class loaders you write yourself are generally not cached either. The cache covers classes loaded by the JDK's built-in loaders: the boot, platform and application loaders.
5. The individual class
Even with a valid cache, some classes are left out when the cache is created. The JVM skips a class if it fails verification, if it uses a class file format too old for the cache to handle, or if it depends on something that cannot be archived. A skipped class loads normally at run time. This is not a rejection of the whole cache. It only means that class gets no speedup.
The decision path
The diagram shows the order in which the JVM decides. Every "no" branch ends with the cache not being used for that run.
Seeing a rejection happen
Start from the working setup of the earlier chapters. You have app.jar and app.aot built from the Hello program. First confirm the cache works:
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloThe line for Hello should report a shared objects file as its source. Now change the timestamp of the JAR without changing its contents:
touch app.jar
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloThis time the source of Hello is the JAR path, not the shared objects file. The program printed the same result, so nothing looked wrong. Only the log shows that the cache was not used.
Next, try a class path that does not have the training path as a prefix. Create a second, empty-purpose JAR and put it first:
jar --create --file extra.jar --main-class Hello Hello.class
java -XX:AOTCache=app.aot -Xlog:class+load -cp extra.jar:app.jar Hello | grep HelloOn Windows, separate class path entries with ; instead of :. The class path no longer starts with app.jar, so the cache is rejected.
To restore a working cache after these experiments, repeat the create step against a fresh record, because touch changed the timestamp the cache remembers.
Asking the JVM why
The class+load log shows the effect. To see the reason, turn on the logging for the cache itself:
java -XX:AOTCache=app.aot -Xlog:cds -Xlog:class+path=info -cp app.jar HelloThe cds tag reports the validation steps and messages such as a mismatch. The class+path tag shows the class path comparison. The exact wording of the messages differs between JDK releases, so search the output for words like "mismatch", "disabled" and "shared" rather than expecting a fixed string.
Making a rejection loud
In the default mode, -XX:AOTMode=auto, a rejected cache is silent. That is the right choice for a tool that must always start. It is a poor choice for a service where you rely on the faster startup, because a stale cache goes unnoticed and your startup time quietly returns to its old value.
Set -XX:AOTMode=on to require the cache:
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloWith on, the JVM prints an error and exits if it cannot use the cache. Use it in these places:
- In integration tests and smoke tests, so a broken cache fails the build.
- In production images where you control the JAR and the JDK, and a mismatch means a packaging mistake.
Do not use it in places where the JAR or the JDK can legitimately vary, such as developer machines. A hard failure there creates noise without protecting anything.
Common causes and fixes
- The build is not reproducible. A CI job rebuilds the JAR after creating the cache, so the timestamp changes. Fix: create the cache from the final JAR, as the last step before packaging.
- A different JDK runs the app. The cache was made on a build machine, and the runtime image uses another JDK build. Fix: use one JDK distribution and version for both, ideally one image.
- The class path differs. A launcher script adds an entry at the front, or a wildcard such as
lib/*expands in a different order on another machine. Fix: use the same explicit class path in training and production. Order matters. - The files are copied without preserving metadata. Copying a JAR with a tool that resets modification times invalidates the cache. Fix: create the cache after the copy, in the final location, or preserve timestamps.
- Layout options differ. The training run used the default heap and production sets a very large one. Fix: train with the production memory settings.
- The cache file is on a read-only or missing path. Fix: check the path and permissions, and confirm it with
AOTMode=on.
Partial use
Acceptance is not always all-or-nothing. If the class path matches but one class was excluded at creation time, the JVM accepts the cache and loads only that class the slow way. Because of this, a cache that reports success can still leave part of your startup unimproved. The training run decides which classes enter the cache, so a cache that is valid but incomplete points back to the training run, not to validation.
Exercise
You create app.aot with -cp app.jar. A colleague deploys it and starts the program with -cp libs/util.jar:app.jar. Startup is no faster, and no error appears. Give the cause, and name the one flag change that would have exposed the problem.
Show answer
The run-time class path no longer begins with the training class path, because libs/util.jar was placed in front of app.jar. The JVM rejects the cache, and the default AOTMode=auto runs without it silently. Starting with -XX:AOTMode=on would have made the JVM print an error and exit. Appending libs/util.jar after app.jar would also have kept the cache valid.
Exercise
After a successful build, your pipeline signs app.jar and copies it into the image, then the cache stops working. The contents did not change. Why, and what should you reorder?
Show answer
Signing or copying changed the file's size or modification time, and the JVM compares both against the values recorded when the cache was created. Create the cache from the final JAR, after signing and copying, in the final location, or preserve the exact file.