Quick revision
The short answer
We first ask jdeps which JDK modules the application needs, with jdeps --print-module-deps --ignore-missing-deps on the application jar and its libraries, and then pass that list to jlink: jlink --add-modules java.base,java.sql,... --output runtime --strip-debug --no-header-files --no-man-pages. The result is a runtime directory with bin/java and only the selected modules, often a fraction of a full JDK. Non-modular application jars are then run on the class path with runtime/bin/java, while a fully modular application can be linked into the image itself with --module-path and given a launcher with --launcher.
What to remember
In this answer, we built a minimal runtime in two steps. jdeps --print-module-deps --ignore-missing-deps listed the JDK modules the application uses, and jlink --add-modules with --strip-debug, --no-header-files and --no-man-pages linked them, plus service-loaded modules jdeps cannot see, into a runtime directory with bin/java and only those modules. We ran non-modular jars on that runtime's class path, and saw that a fully modular application can be linked into the image itself with --module-path and a --launcher.
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 first ask jdeps which JDK modules the application needs, with jdeps --print-module-deps --ignore-missing-deps on the application jar and its libraries, and then pass that list to jlink: jlink --add-modules java.base,java.sql,... --output runtime --strip-debug --no-header-files --no-man-pages. The result is a runtime directory with bin/java and only the selected modules, often a fraction of a full JDK. Non-modular application jars are then run on the class path with runtime/bin/java, while a fully modular application can be linked into the image itself with --module-path and given a launcher with --launcher.
Why this question is asked
A full JDK contains everything: compilers, tools, desktop libraries, scripting support and dozens of modules most server applications never touch. Shipping all of that in every container image costs disk, download time and attack surface. jlink, added in Java 9 together with the module system, builds a custom runtime that contains only what an application needs. Interviewers ask this to see whether we can actually do it, and whether we know the details that make it work in practice, such as modules jdeps cannot see. In this answer, we will recap what a runtime image is, find the modules with jdeps, link the runtime with jlink, look at what the image contains, run a class path application on it, and then link a fully modular application with its own launcher.
1A Quick Recap: What a Runtime Image Is
Since Java 9, the JDK itself is made of modules: java.base with the core classes, java.sql for JDBC, java.net.http for the HTTP client, java.desktop for Swing and AWT, and around seventy in total, depending on the version. A Java runtime is a directory with a java launcher and a set of these modules stored in an optimised format in a file called lib/modules.
jlink is the tool that assembles such a runtime from a chosen set of modules. Because each module declares what it requires, jlink can take a short list of the modules we need, add everything they depend on, and produce a self-contained runtime with nothing else in it.
2Find the Needed Modules With jdeps
jdeps is the JDK's dependency analyser. It reads class files and reports which packages and modules they use. With --print-module-deps it prints just the comma-separated list of JDK modules we need.
--ignore-missing-deps stops jdeps from failing when a library refers to classes that are not present, such as optional dependencies of a logging library, which is very common. --multi-release tells it which version of multi-release jars to analyse. Notice that jdeps only sees static references in bytecode. Anything loaded by name at run time, through reflection or ServiceLoader, is invisible to it, and that is where most jlink surprises come from, as we will see.
3Link the Runtime With jlink
Now we hand that list to jlink. It resolves the modules and their dependencies and writes the runtime to a new directory.
- jdeps --print-module-deps on app and librariesstatic references only
- Add service-loaded modules by hand
jdk.localedata,jdk.charsets, crypto - jlink --add-modules ... --output runtimestrip debug, headers, man pages
- Is the application fully modular?YesLink it in with --module-path and --launcherNoRun its jars on the class path
- Test the real application on the imageTLS calls, locales, monitoring
5 steps in order. Play them, or step through them one at a time.
Notice that the list printed by --list-modules is longer than what we asked for: jlink added the modules our modules require, such as java.xml and java.transaction.xa, which java.sql requires. --strip-debug removes debugging information from the classes, and --no-header-files and --no-man-pages drop files a server never uses. Notice also the two modules we added by hand. jdk.crypto.ec provided elliptic-curve cryptography for TLS on older JDKs, and its classes moved into java.base in newer ones, so check your version; jdk.localedata holds locale data beyond English, and without it date and number formatting for other locales quietly falls back. jdeps found neither, because both are used through service lookup, not direct references. Other common additions are jdk.charsets, jdk.zipfs and jdk.management for monitoring. The exact size depends on the modules and JDK version.
4What the Image Contains
The output directory looks like a small JDK without the development tools.
So there is no javac, no jshell and no jar tool unless we explicitly added their modules, and none of the unused JDK modules. The release file is handy in a container image: cat runtime/release shows exactly which modules a deployed runtime contains.
5Running a Class Path Application on the Image
Most applications, including Spring Boot services, are not modular. That is fine: the custom runtime is a normal Java runtime, and we run the application jars on its class path, as with any JDK.
If a module is missing, the symptom is usually a NoClassDefFoundError or ClassNotFoundException for a java.* or javax.* class at run time, for example java/beans classes from java.desktop, which some frameworks use. That is why the image must be tested with the real application, including TLS calls and the locales it serves, not just started.
6Linking a Fully Modular Application With a Launcher
If the application and all its dependencies are named modules with real descriptors, jlink can include them in the image too, and generate a launcher script so nobody needs to remember the module and main class.
Here jlink starts from our root module, follows its requires, and links our modules, their dependencies and the JDK modules they need, in one image. The catch is that every module must have a descriptor: jlink refuses automatic modules, because their dependencies are unknown. Since JDK 9 the JDK's own jmods directory supplied the JDK modules automatically; newer JDKs can also link from the running JDK's own image when jmods are not shipped, a change from JDK 24, so check your vendor's build.
7Follow-up questions
Why not just use a slim JRE base image instead?
A slim distribution still contains every runtime module. jlink removes the ones we do not use, which makes the image smaller and reduces what a vulnerability scanner reports. The cost is that we own the module list and must update it when the application starts using something new.
Can the same runtime serve several services?
Yes, if we link the union of their modules, which is a common compromise: one company base image with a runtime that covers typical Spring services, built once per JDK update, rather than one image per service.
8Conclusion
In this answer, we built a minimal runtime in two steps. jdeps --print-module-deps --ignore-missing-deps listed the JDK modules the application uses, and jlink --add-modules with --strip-debug, --no-header-files and --no-man-pages linked them, plus service-loaded modules jdeps cannot see, into a runtime directory with bin/java and only those modules. We ran non-modular jars on that runtime's class path, and saw that a fully modular application can be linked into the image itself with --module-path and a --launcher.
Open with
I would do it in two steps. First jdeps: I run jdeps --print-module-deps --ignore-missing-deps on the application jar with its libraries on the class path, and it prints the JDK modules the code references, something like java.base, java.logging, java.naming, java.sql.
Then, if they want more
The ignore flag matters because libraries often reference optional classes that are not there. I know jdeps only sees static references, so I add modules that are loaded through services or reflection, like jdk.localedata for locales, jdk.charsets, or crypto modules depending on the JDK version.
Second jlink: jlink --add-modules with that list, --output runtime, and --strip-debug, --no-header-files and --no-man-pages to drop what a server does not need, plus compression. jlink resolves the dependencies of those modules, so the image includes things like java.xml because java.sql needs it.
The result is a directory with bin/java, conf, legal, lib/modules containing only the selected modules, and a release file listing them, typically tens of megabytes instead of hundreds.
Most applications are not modular, so I just run their jars on the class path with runtime/bin/java, and in Docker I build the runtime in one stage and copy only it into the final image.
If the application and all dependencies are real named modules, I can link them in too with --module-path and add a launcher with --launcher, but jlink refuses automatic modules.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 21, Java 24
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Show how to build a minimal custom runtime for an application with jdeps and jlink, and...": https://scrivio-interview-library.netlify.app/java/modules/a3ff78d010/