Sub topic 6
Finding the modules you need with jdeps
This chapter assumes you can compile a modular Java project. It also assumes you know what module-info.java and requires do, and that you can run javac and jar from the command line.
Why ask a tool
jlink only includes the modules you name with --add-modules, plus everything those modules require. If you leave one out, the image fails at run time with a NoClassDefFoundError. If you add too many, the image is larger than it needs to be.
jdeps is the JDK's dependency analyzer. It reads compiled class files and reports which packages and modules they use. You can use that report to build the --add-modules list.
Running jdeps on a modular JAR
Take the application from the earlier chapters. Its descriptor declares requires java.logging;, and Main uses java.util.logging.Logger.
mkdir -p out/com.example.app mods
javac -d out/com.example.app \
src/module-info.java src/com/example/app/Main.java
jar --create --file mods/app.jar \
--main-class com.example.app.Main \
-C out/com.example.app .
jdeps --print-module-deps mods/app.jarThe last command prints one line of comma-separated module names, such as java.base,java.logging. You can paste this line directly into --add-modules. For a modular JAR, jdeps reads the descriptor and also checks the bytecode, so the result matches what the code uses.
Two other views are useful when you want to see more detail:
jdeps -summary mods/app.jarshows one line per dependency, from the JAR to each module.jdeps mods/app.jarlists every package used and the module that provides it.
Analyzing classpath JARs
Many applications have no module-info.class. jdeps still analyzes them, but a few options matter:
jdeps --print-module-deps \
--ignore-missing-deps \
--multi-release 17 \
lib/app.jar lib/dependency.jar--ignore-missing-depsstopsjdepsfrom failing when a class refers to a library you did not pass in. Without it,jdepsreports an error and may print no module list.--multi-release 17selects which version of a multi-release JAR to analyze. Without it,jdepsstops with an error when it meets such a JAR.- List every JAR you want analyzed. Dependencies you leave out are not scanned, and the modules they need will be missing from the result.
What jdeps cannot see
jdeps analyzes static references in bytecode. It misses anything that is resolved at run time:
- Classes loaded by name with
Class.forNameor reflection. - Service providers found through
ServiceLoader. These need--bind-services, covered in a later chapter. - Modules used only by an optional feature, such as a JDBC driver or a charset provider that your code never names.
Checking against the JDK
To see all modules your JDK offers, run java --list-modules. You can compare your jdeps output with this list to confirm the names are valid. Names such as java.logging, java.sql, and jdk.httpserver all appear there.
Exercise
You run jdeps --print-module-deps on a classpath JAR and the command fails because the JAR calls a library you did not pass in. Which option lets the analysis finish, and why might the resulting list still be incomplete?
Show answer
Add --ignore-missing-deps. The analysis then skips classes it cannot resolve, so any JDK modules needed only by the missing library, and any modules reached through reflection or ServiceLoader, will not be in the list. Pass all dependency JARs, and test the final image.