Sub topic 6
Finding the modules you need with jdeps
This chapter assumes you can compile and run Java programs with javac and java, and that you know what a JAR and the classpath are. It also assumes you have read the previous chapters on module-info.java and the JDK's modules. Here you will learn how to ask the JDK which of its modules your application uses, so you can give jlink an accurate list.
Why you need an analysis tool
jlink includes only the modules you name, plus everything those modules require. If you list too few, the image fails at run time with a NoClassDefFoundError or ModuleNotFoundException. If you list too many, the image is larger than it needs to be.
For a small modular application, module-info.java already tells you the answer. For a larger one, or one built from many JARs, nobody wants to read every descriptor and every import by hand. jdeps is the JDK's static dependency analyzer. It reads compiled class files and reports which packages and modules they reference.
The dotted line marks the weak spot of the whole approach, which this chapter returns to below. jdeps only sees what is in the bytecode.
Running jdeps on the example application
The earlier chapters built mods/app.jar from a module that requires java.logging. This is the same command from that build, run against the JAR:
jdeps --print-module-deps mods/app.jarThe output is a single comma-separated line of module names. It contains java.logging, and may also list java.base. That line is designed to be pasted straight into jlink:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules "$(jdeps --print-module-deps mods/app.jar)",com.example.app \
--output build/runtimeThe $(...) substitution turns the jdeps output into the --add-modules value. You still add your own application module by name, because jdeps reports only the JDK modules your code depends on, not your code itself.
The output modes of jdeps
jdeps has several reporting modes. They answer different questions, and mixing them up is a common source of confusion.
| Option | What it prints | Typical use |
|---|---|---|
| `--print-module-deps` | One comma-separated line of module names | Feeding `jlink --add-modules` |
| `--list-deps` | One module or package per line | Reading the list yourself |
| `--list-reduced-deps` | Like `--list-deps`, but omits modules already implied by another listed module | Seeing the minimal set |
| `-summary` (or `-s`) | One line per archive-to-module dependency | Quick overview |
| `-verbose:class` | Class-to-class dependencies | Finding which class pulls in a module |
| `--generate-module-info dir` | A draft `module-info.java` for each JAR | Describing non-modular JARs |
A summary run looks similar to this. The exact archive name depends on your build:
app.jar -> java.base
app.jar -> java.loggingThe reduced mode matters because modules depend on each other. If your code uses java.sql, that module requires java.logging and java.xml transitively. jlink resolves those for you, so you do not have to list them. --print-module-deps prints the modules your code references directly, and --list-reduced-deps removes the ones another listed module already covers.
Analyzing an application with a classpath
Many real applications are not modular. They are a main JAR plus a directory of library JARs on the classpath. Point jdeps at the main JAR and tell it where the libraries are:
jdeps --class-path 'libs/*' \
--multi-release 17 \
--ignore-missing-deps \
--print-module-deps \
target/app.jarEach option has a reason:
--class-path 'libs/*'letsjdepsfollow references into the library JARs. Without it, every library class is reported as "not found". The quotes stop your shell from expanding the*.--multi-release 17selects which versioned section of multi-release JARs to read. Many libraries ship these, andjdepsstops with an error if you do not choose a version. Use the Java version you will run on.--ignore-missing-depssuppresses the error for classes that cannot be found anywhere. Libraries often reference optional packages that are not present, and this keeps those references from stopping the analysis.
The last point has a cost. A genuinely missing class is silenced in the same way as an optional one. Run once without the flag and read the warnings before you decide they are harmless.
The result covers the modules needed by the main JAR and all the libraries it can reach through the class path. You then pass the list to jlink, along with the handling for non-modular code covered in a later chapter.
What jdeps cannot see
jdeps is static analysis. It reads bytecode and reports references that appear there. It cannot report anything your program decides at run time. These cases are the usual ones to miss:
- Reflection by name.
Class.forName("java.sql.Driver")contains only a string, sojdepsdoes not reportjava.sql. - Services. A library that finds implementations with
ServiceLoadermay need modules your code never mentions.jlinkincludes service providers only if you ask it to bind them. The service chapter covers this. - Optional modules loaded by configuration. A logging framework might load a JMX or XML component only if a setting is on.
- Modules needed only by tools you will use later. For example,
jdk.jcmdorjdk.management.agentare not referenced by your code, but you may want them for diagnostics.
Because of this, treat the jdeps output as a minimum, not a final answer. Add modules for any of the cases above that apply to your application. A frequent example is adding jdk.unsupported for libraries that use sun.misc.Unsafe, or jdk.crypto.ec for TLS with elliptic curve ciphers. Missing crypto providers are a well-known source of runtime failures that only appear when a connection is made.
Using jdeps to explain a dependency
Sometimes the list contains a module you did not expect, and you want to know why. Class-level output answers this:
jdeps -verbose:class --class-path 'libs/*' target/app.jar | grep "java.sql"Each matching line shows a class in your code or a library and the JDK package it references. From there you can decide whether to replace the library, accept the larger image, or exclude the dependency.
You can also see what a single JDK module contains. java --describe-module java.sql prints the module's requires and exports, and java --list-modules lists every module in the JDK you are running. The second command is the same one you will use later to check the finished image.
Checking the result
After you build with the discovered list, run the application through the image and exercise as many code paths as you can, not just startup. Static analysis catches the common case. Testing catches the rest. Use build/runtime/bin/java --list-modules to confirm the image holds what you expect. If a feature fails with a missing class, add the module and rebuild.
Summary of the working model
jdeps reads bytecode and reports the JDK modules it references. --print-module-deps formats that report for jlink. A classpath, a multi-release version, and a decision about missing dependencies are the three settings that most often need adjusting for real applications. Whatever the tool cannot see, you must add by hand.
Exercise
Your application calls Class.forName("java.sql.DriverManager") and nothing else from java.sql. You run jdeps --print-module-deps on its JAR and build an image with the result. At run time the call fails with a missing class error. Why did jdeps not report java.sql, and what are two ways to fix it?
Show answer
jdeps analyzes bytecode references, and a class name passed as a string to Class.forName is not a reference it can follow. Fixes: add java.sql by hand with --add-modules (for example appended to the jdeps output), or change the code to reference the class directly so the dependency appears in the bytecode. Either way, test the image to confirm.
Exercise
What is the difference between --list-deps and --list-reduced-deps, and which would you choose to read the minimal set of modules by eye?
Show answer
--list-deps prints every module the code references, one per line. --list-reduced-deps removes modules that are already required by another module in the list, because jlink adds those automatically. For reading the minimal set, --list-reduced-deps is the better choice.