Quick revision
The short answer
First we extract Boot's executable jar into plain jars, because CDS cannot archive classes loaded from jars nested inside another jar; since Boot 3.3, java -Djarmode=tools -jar app.jar extract does this. Then we do a training run with -XX:ArchiveClassesAtExit=app.jsa, using -Dspring.context.exit=onRefresh so the application exits after the context starts, and run production with -XX:SharedArchiveFile=app.jsa. The archive only works with the same JDK build and a compatible class path, JDK 19 added -XX:+AutoCreateSharedArchive to manage it automatically, and we measure startup with and without it.
What to remember
In this answer, we created an AppCDS archive for a Spring Boot application. We extracted the executable jar with java -Djarmode=tools -jar app.jar extract, because CDS cannot archive classes from nested jars, ran a training run with -XX:ArchiveClassesAtExit and -Dspring.context.exit=onRefresh while avoiding side effects, and started production with -XX:SharedArchiveFile. We saw that the archive is valid only for the same JDK build and a compatible class path, that JDK 19's -XX:+AutoCreateSharedArchive can maintain it automatically, that training belongs in the image build, and that we confirm the gain by measuring startup with and without the archive.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we extract Boot's executable jar into plain jars, because CDS cannot archive classes loaded from jars nested inside another jar; since Boot 3.3, java -Djarmode=tools -jar app.jar extract does this. Then we do a training run with -XX:ArchiveClassesAtExit=app.jsa, using -Dspring.context.exit=onRefresh so the application exits after the context starts, and run production with -XX:SharedArchiveFile=app.jsa. The archive only works with the same JDK build and a compatible class path, JDK 19 added -XX:+AutoCreateSharedArchive to manage it automatically, and we measure startup with and without it.
Why this question is asked
AppCDS is one of the cheapest startup improvements a Spring Boot service can get: no code changes, a couple of extra build steps, and often a large share of class loading time saved. But the details matter, and the most common result of a first attempt is an archive that the JVM silently ignores. Interviewers ask for the steps to see whether we know why each step exists. In this answer, we will see why the executable jar must be extracted, go through the training run and the production run, look at the compatibility rules that make archives fail, and finish with automatic archives, container builds and measurement.
1Why Boot's Executable Jar Must Be Extracted
CDS archives classes the JVM loads from the class path or module path, and it identifies them by the jar files they came from. A Spring Boot executable jar is different: it contains the application's own classes plus every dependency as nested jars inside BOOT-INF/lib, and a special Spring class loader reads classes out of those nested jars. The JVM's CDS machinery cannot archive classes loaded that way.
So the first step is to turn the fat jar into a layout the JVM understands: a small application jar whose manifest class path points to the dependency jars sitting next to it in a lib folder. Since Spring Boot 3.3, the jar includes a tools mode that does exactly this.
Note that CDS archives classes from jar files; the class path used for archiving should list jars, not directories of loose class files. The extracted layout gives us exactly that.
2The Training Run
An archive records the classes that were loaded during a run, so we need a run that loads the classes production will load. For startup, the main ones are the classes loaded while the Spring context starts. Spring Framework supports a property that makes the application exit as soon as the context has been refreshed, which gives a short, repeatable training run.
ArchiveClassesAtExit creates a dynamic archive, layered on top of the JDK's default archive, containing the application and library classes loaded during the run. There is a catch: context refresh really starts the application. Beans are created, and things like Flyway or Liquibase migrations, cache warmers or message listeners may run. So the training run should use a profile that points at a disposable database or disables side effects, while still loading the same classes.
3Running With the Archive
Production starts the same extracted jar, with the same JDK and the same class path, and points the JVM at the archive.
6 messages in order. Play them, or step through them one at a time.
The training JVM and the production JVM must use the same JDK build and class path.
If the archive is used, the class loading log shows application and library classes coming from the shared objects file, not only JDK classes. If it is not, the default -Xshare:auto mode prints a warning and carries on without it, which is why we must check rather than assume.
4Why Archives Get Ignored
An archive is a snapshot of internal JVM structures, so it is only valid for the exact JDK build that created it. A different JDK version or vendor build, even a patch update, makes the JVM reject it. The class path must also match: the class path at runtime must start with the same jars, in the same order, as at archive time, and the jars must not have changed since. Some option changes, such as switching to a different garbage collector, can also prevent parts of the archive from being used, depending on the JDK version.
In practice this means the archive must be created in the same build that produces the final image, on the same base image, and never copied between images with different JDKs. If we create the archive on a developer laptop and run it in a container, it will almost certainly be ignored.
5Automatic Archives and Container Builds
JDK 19 added -XX:+AutoCreateSharedArchive. Used together with -XX:SharedArchiveFile, it makes the JVM use the archive if it is valid and, if it is missing or stale, for example after a JDK update, create a fresh one when the JVM exits. That removes the separate training step, at the cost of the first run after a change being unarchived.
For containers, training inside the image build is usually better than AutoCreateSharedArchive, because images are immutable and every new pod would otherwise pay the unarchived start. Cloud Native Buildpacks used by Spring Boot can also do the training run for us when CDS is enabled in the build configuration (check the option name for your buildpack version). On JDK 24 and later, the AOT cache is the successor of this mechanism and stores more startup work, and recent Boot versions document it as an alternative.
6Measuring the Gain
Finally, we measure, because the benefit depends on the application. Spring Boot logs its own startup time, which makes the comparison easy: start the same image several times with and without the archive and compare the Started ... in N seconds line, or measure time to the first successful readiness probe. Many Spring services see startup time drop noticeably, but the exact gain varies, and it is worth recording the result so that we notice if a later change, such as a JDK upgrade without retraining, quietly loses it.
7Follow-up questions
What is the difference between a static and a dynamic archive?
A static archive is created from a class list with -Xshare:dump and contains all listed classes, including JDK classes. A dynamic archive, created with ArchiveClassesAtExit, contains only the classes not already in the base archive and is layered on top of the JDK's default archive. Dynamic archives are simpler to create and are what most applications use.
Does the training run need to exercise request handling?
For startup, the context refresh covers most classes. Classes first loaded when handling requests are not in the archive and load normally, which is fine. A training run that also sends a few typical requests includes them, at the cost of a longer and more complex training step.
Will AppCDS work with Spring Boot's AOT processing or GraalVM native image?
Spring's AOT processing on the JVM generates code ahead of time and combines well with CDS, since both reduce startup work. GraalVM native image is a different approach that compiles to a native executable, so CDS does not apply to it.
8Conclusion
In this answer, we created an AppCDS archive for a Spring Boot application. We extracted the executable jar with java -Djarmode=tools -jar app.jar extract, because CDS cannot archive classes from nested jars, ran a training run with -XX:ArchiveClassesAtExit and -Dspring.context.exit=onRefresh while avoiding side effects, and started production with -XX:SharedArchiveFile. We saw that the archive is valid only for the same JDK build and a compatible class path, that JDK 19's -XX:+AutoCreateSharedArchive can maintain it automatically, that training belongs in the image build, and that we confirm the gain by measuring startup with and without the archive.
Open with
The first step is to extract the executable jar, because Boot's fat jar loads dependencies from nested jars through its own class loader, and CDS can only archive classes from plain jars on the class path.
Then, if they want more
Since Boot 3.3, I run java -Djarmode=tools -jar app.jar extract, which gives me a small application jar plus a lib folder of dependency jars. Then I do a training run from the extracted layout with -XX:ArchiveClassesAtExit=app.jsa and -Dspring.context.exit=onRefresh, so the application starts its context, exits, and the JVM writes a dynamic archive of the classes it loaded.
I make sure the training run uses a profile without side effects, because refresh really runs things like migrations and listeners. In production I start with -XX:SharedArchiveFile=app.jsa and verify with -Xlog:cds or the class loading log that application classes come from the shared archive.
The archive must be used with the same JDK build and a matching class path; otherwise, under the default -Xshare:auto, the JVM just warns and ignores it, so I create it in the same image build that ships the application. Since JDK 19 there is also -XX:+AutoCreateSharedArchive, which recreates a stale archive automatically.
Finally, I compare startup time with and without the archive to confirm the gain.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 19, Java 24, Spring Boot 3.3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe the steps to create and use an AppCDS archive for a Spring Boot application...": https://scrivio-interview-library.netlify.app/java/startup-and-footprint/ae91c78c2a/