Sub topic 11
Building the cache in CI and container images
This chapter assumes you can run the record, create and run workflow from earlier chapters. It also assumes you know the one-step flag from JDK 25 and have basic Docker and CI experience. The question here is where the cache gets built so that it is valid when the application starts in production.
The cache belongs to a specific build
The JVM validates a cache against the environment that created it. The JDK build must match, and so must the classpath and the jar files on it. A cache created on a laptop will usually be rejected on a server. Three rules follow:
- Build the cache with the same JDK that will run the application.
- Build it after the final jar exists, and don't touch the jar afterward.
- Build it at the same path where the jar will live at runtime.
In practice the cache is a build artifact for one specific deployment. It is not a source file, so don't commit it to version control.
Where it fits in a pipeline
The training run happens late, inside the image that will be shipped. That is the simplest way to satisfy the three rules, because the JDK, the jar path and the jar file are the ones used at runtime.
A container image example
This Dockerfile compiles in one stage. In the final stage it copies the jar and runs the training run as a build step, so the cache is stored in an image layer. It uses the Hello program from earlier in this tutorial.
FROM eclipse-temurin:25-jdk AS build
WORKDIR /src
COPY Hello.java .
RUN javac Hello.java \
&& jar --create --file app.jar --main-class Hello Hello.class
FROM eclipse-temurin:25-jdk
WORKDIR /app
COPY --from=build /src/app.jar /app/app.jar
RUN java -XX:AOTCacheOutput=/app/app.aot -jar /app/app.jar
ENTRYPOINT ["java", "-XX:AOTMode=on", "-XX:AOTCache=/app/app.aot", "-jar", "/app/app.jar"]Some details matter here:
- Both the training run and the entrypoint use
-jar /app/app.jar. The classpath is the same in both. - The final stage uses a full JDK image. A different runtime image can have a different JVM build, and the cache would then be rejected. If you want a smaller runtime image, check that the cache still loads with
-XX:AOTMode=onbefore you ship it. - The cache is created on the same CPU architecture the image runs on. For multi-architecture images, the build step runs once per platform, so each platform gets its own cache.
Real applications need a real training run
Hello finishes by itself. A service does not, so the training command must exit on its own. Common approaches:
- Add a flag or environment variable that starts the application, runs a few representative requests or a health check, and then shuts down.
- Start the application, call it from a script, then stop it with a signal. Check that the JVM exits cleanly, because the cache is written at exit.
- Point the training run at test configuration. It should not need a production database or secrets, because those are not available during an image build.
The training run decides which classes are cached, so it should exercise the same code paths as production startup. Chapter material on choosing a training run applies unchanged. CI is simply where you encode it as a script.
Building in CI without containers
When you deploy a jar to hosts instead of an image, run the same three steps in the pipeline:
set -euo pipefail
# Build the jar (your build tool here), then:
java -XX:AOTCacheOutput=build/app.aot -jar build/app.jar --training
# Verify that the cache is usable before publishing it
java -XX:AOTMode=on -XX:AOTCache=build/app.aot -jar build/app.jar --trainingThe --training argument is an example of an application flag that runs a short workload and exits. The second command is a gate. If the cache is not usable, the JVM exits with an error and the pipeline fails. Publish app.jar and app.aot together as one unit. On the deployment host, the jar must sit at the same path it had when the cache was built.
On JDK 24, replace the first command with the record and create steps from the earlier chapter.
Caching in CI
If CI restores caches between runs, key any stored app.aot on the JDK version and a hash of the jar. Reusing a cache after either one changes only produces a rejected cache. In most pipelines it is cheaper to rebuild, since the training run usually takes seconds.
Exercise
A team builds app.aot on a CI runner with JDK 25 and copies it, together with app.jar, into an image based on a different JDK 25 vendor build. Startup is no faster, and there is no error. What happened, and what two changes would fix it and make future failures visible?
Show answer
The JVM rejected the cache because it was created by a different JDK build, then ran without it. To fix it, create the cache inside the final image with the same JDK that runs the application. To make future failures visible, start the application with -XX:AOTMode=on so that an unusable cache stops startup with an error.