Quick revision
The short answer
We run jdeps --print-module-deps --ignore-missing-deps over the application and its libraries to list the JDK modules they use, then jlink --add-modules with that list, plus --strip-debug, --no-header-files and --no-man-pages, to build a runtime containing only those modules. It makes the image much smaller and reduces the attack surface, but resident memory barely changes. We must add modules that jdeps cannot see, such as those used through reflection or service loading, keep diagnostics modules like jdk.jcmd, jdk.jfr and jdk.management.agent, and rebuild the runtime for every JDK security update.
What to remember
In this answer, we used jdeps --print-module-deps --ignore-missing-deps to list the JDK modules an application references and jlink with --strip-debug, --no-header-files and --no-man-pages to build a runtime with just those modules. It shrinks the image and the attack surface but not resident memory. Modules used through reflection, service loading or locale data must be added by hand, diagnostics modules should stay, and the runtime must be rebuilt for every JDK security update.
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.
We run jdeps --print-module-deps --ignore-missing-deps over the application and its libraries to list the JDK modules they use, then jlink --add-modules with that list, plus --strip-debug, --no-header-files and --no-man-pages, to build a runtime containing only those modules. It makes the image much smaller and reduces the attack surface, but resident memory barely changes. We must add modules that jdeps cannot see, such as those used through reflection or service loading, keep diagnostics modules like jdk.jcmd, jdk.jfr and jdk.management.agent, and rebuild the runtime for every JDK security update.
Why this question is asked
A full JDK in a container image is often over 300 MB, and most of it is never used by a web service: compilers, desktop toolkits, documentation and dozens of tools. Since Java 9, the JDK is split into modules, and jlink can assemble a runtime from just the modules we need. Interviewers ask this as a coding question because the commands are short, but using them safely needs judgement about what jdeps cannot see and what operations still need. In this answer, we will recap modules, find the needed modules with jdeps, build the runtime with jlink, look honestly at what it saves, list the modules we must add by hand, and finish with diagnostics and updates.
1A Quick Recap of JDK Modules
Since Java 9, the JDK is made of modules such as java.base (the core classes every program needs), java.sql (JDBC), java.net.http (the HTTP client) and jdk.jfr (Flight Recorder). A module declares which other modules it requires, so the set of modules a program needs can be worked out.
jdeps is a tool that reads class files and reports what they depend on, including which JDK modules. jlink is a tool that takes a list of modules and writes a runtime image, a directory with a java launcher and only those modules. The application itself does not need to be modular for this; jlink only builds the runtime, and we still run our jars on the class path.
2Finding the Modules With jdeps
For a Spring Boot fat jar, we first extract the jars, because jdeps needs to see the libraries as plain jars. Then we ask jdeps for the module list.
Notice the options. --print-module-deps prints a comma-separated list ready to pass to jlink. --ignore-missing-deps stops jdeps from failing on optional classes that libraries reference but we do not ship, which is normal in Spring applications. --multi-release picks the right classes from multi-release jars. The list often surprises people with java.desktop; Spring uses the java.beans package, which lives in that module.
3Building the Runtime With jlink
Now we give jlink that list, together with modules we know jdeps missed, and options that remove parts a server never needs.
- Extract the application jarsjarmode tools extract
- jdeps --print-module-depsmodules the code references
- Add hidden modulesreflection, service loading, locales
- Add diagnostics modules
jdk.jcmd,jdk.jfr,jdk.management.agent - jlink with strip optionsruntime in /opt/runtime
- Run the full test suite on that runtimecatches a missing module
6 steps in order. Play them, or step through them one at a time.
--strip-debug removes debug information from the class files, --no-header-files and --no-man-pages drop C headers and manual pages, and --compress shrinks the module image. The compression syntax changed in JDK 21 from numbered levels such as --compress=2 to names such as zip-6, so check your version. The extra modules on the first line are the ones jdeps did not report, explained in the next sections.
4What It Saves and What It Does Not
The savings are on disk and in the attack surface. A smaller image pulls faster onto new nodes, costs less in the registry, and contains fewer tools and libraries that a security scanner has to report and an attacker could use. For a fleet of many services, that adds up.
What it does not save is resident memory. The JVM only loads the classes the program uses, and unused modules on disk were never in memory in the first place. A service that used 350 MB of RSS on a full JDK will use roughly the same on a jlinked runtime. If the goal is memory, heap sizing, threads and dependencies matter far more.
5Modules jdeps Cannot See
jdeps reads static references in class files. Anything a program finds by name at run time is invisible to it, and the failure only appears when that code path runs, sometimes in production.
| Module | Used for | Symptom if missing |
|---|---|---|
jdk.crypto.ec | Elliptic-curve TLS on JDKs before 22 | TLS handshake failures |
jdk.crypto.cryptoki | PKCS#11 security providers | Provider not found |
jdk.localedata | Non-English locale data | Wrong date and number formats |
jdk.zipfs | Zip file system provider | ProviderNotFoundException |
jdk.naming.dns | DNS lookups through JNDI | Naming errors |
jdk.charsets | Extra character sets | UnsupportedCharsetException |
java.base; check your version.The fix is to add these explicitly and to run the full test suite, including TLS connections and locale-sensitive code, on the jlinked runtime rather than on a full JDK.
6Keeping Diagnostics and Owning Updates
It is tempting to drop every tool, but the first production incident will need them. jdk.jcmd gives jcmd for thread dumps, heap dumps and JFR control, jdk.jfr gives Flight Recorder, and jdk.management.agent lets us attach JMX remotely. Their size is small compared with what they save during an incident. Teams that insist on the smallest possible image can keep a second, debug image with the tools, built from the same pipeline.
The last point is ownership. With a vendor's JDK image, a security update means pulling a new base image. With our own jlinked runtime, nobody else rebuilds it: every JDK update requires rebuilding the runtime from the new JDK and rebuilding every image that uses it. That should be automated in the pipeline, with the JDK version pinned and updated by a bot, or the runtime will quietly fall behind on security fixes.
7Follow-up questions
Do we need a modular application to use jlink?
No. jlink only builds the runtime from JDK modules. Our application and its libraries stay on the class path as normal jars. A fully modular application could also be linked into the image, but that is rarely practical for Spring Boot services.
Why not just use a JRE image from a vendor?
That is a reasonable middle ground: vendor JRE images remove development tools and are rebuilt by the vendor for every update. jlink goes further by removing modules the service does not use, at the cost of owning the build. Many teams start with a JRE image and only jlink when image size really matters.
How do we know the runtime is complete?
By running the full test suite and a smoke test on the jlinked runtime, including TLS calls, locale-dependent formatting and any plugin or provider loading. jdeps gives a starting list; tests prove it.
8Conclusion
In this answer, we used jdeps --print-module-deps --ignore-missing-deps to list the JDK modules an application references and jlink with --strip-debug, --no-header-files and --no-man-pages to build a runtime with just those modules. It shrinks the image and the attack surface but not resident memory. Modules used through reflection, service loading or locale data must be added by hand, diagnostics modules should stay, and the runtime must be rebuilt for every JDK security update.
Open with
First I would extract the Spring Boot jar so the libraries are plain jars, then run jdeps with print-module-deps, ignore-missing-deps, recursive and multi-release over the application and its lib folder.
Then, if they want more
That prints a comma-separated list of JDK modules, usually java.base, java.sql, java.naming, java.management and a few others, including java.desktop because Spring uses java.beans. Then jlink with add-modules and that list, plus strip-debug, no-header-files and no-man-pages and a compress option, whose syntax depends on the JDK version, writing the runtime to a folder that the image copies.
It typically shrinks the runtime from over 300 MB to well under 100, which means faster pulls and fewer things for scanners and attackers, but resident memory barely changes, because unused modules were never loaded anyway.
The care points: jdeps only sees static references, so modules used through reflection or service loading are missed, like jdk.crypto.ec for elliptic-curve TLS on JDKs before 22, jdk.localedata, jdk.zipfs or jdk.charsets, so I add them and run the full tests on that runtime. I keep jdk.jcmd, jdk.jfr and jdk.management.agent for incidents.
And we now own the runtime, so the pipeline must rebuild it on every JDK security update.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 21
- 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 "Build a minimal Java runtime for a service with jdeps and jlink. What commands do you...": https://scrivio-interview-library.netlify.app/java/startup-and-footprint/37a060432b/