Sub topic 17
Analyzing dependencies with jdeps
This chapter assumes you can write a module-info.java, know the difference between the classpath and the module path, and have seen automatic modules and the unnamed module. jdeps is the JDK's static dependency analyzer. It reads compiled .class files and jars and reports what they depend on. It is most useful during migration, and again when you size a runtime image with jlink.
What jdeps does
jdeps inspects bytecode. It does not run your program. For each class it records the types referenced in field types, method signatures, superclasses, and method bodies. It then groups those references by package, jar, or module.
Because the analysis is static, it has one limit that matters throughout this chapter. It cannot see dependencies that appear only at run time, such as Class.forName("...") with a computed name, or a service provider found by ServiceLoader.
Reading the default report
Run jdeps on a jar with no options:
jdeps app.jarThe output is abbreviated and its exact layout varies slightly between JDK versions. It looks like this:
app.jar -> java.base
app.jar -> java.sql
app.jar -> not found
com.example.app -> com.fasterxml.jackson.databind not found
com.example.app -> java.lang java.base
com.example.app -> java.sql java.sqlEach line has the form source package -> target package, followed by where the target was found. Three kinds of target appear:
- A JDK module name, such as
java.sql. - A jar name, when the dependency was found on the class path you gave.
not found, whenjdepscould not locate the class.
not found is usually the most useful finding. It means the dependency is not on the path you supplied, so give jdeps the missing jars.
Choosing the level of detail
Three options control how much jdeps prints.
| Option | What it shows |
|---|---|
| `-s` or `--summary` | One line per archive, such as `app.jar -> java.sql` |
| default | Package-to-package dependencies |
| `-verbose:class` | Class-to-class dependencies |
Start with -s to see the overall shape. Switch to -verbose:class when you need to find which class creates a particular dependency, for example before you split a split package.
Telling jdeps where dependencies live
Point jdeps at your other jars so that not found entries resolve:
jdeps -s --class-path 'libs/*' app.jar
jdeps -s --module-path mods --add-modules ALL-MODULE-PATH -m com.example.app--class-path (or -cp) supplies classpath jars. --module-path supplies modules, and -m starts analysis from a named module. Add -R (--recursive) to follow dependencies transitively into the jars you listed.
Jars that ship multiple versions of a class under META-INF/versions need --multi-release 17 (or whichever version you target). Without it, jdeps may report an error about a multi-release jar.
Finding JDK internal API use
The first migration blocker is usually code that calls JDK-internal classes. These are in packages that are not exported, such as sun.misc or com.sun.*. Ask for them directly:
jdeps --jdk-internals --class-path 'libs/*' app.jarThe report lists each offending class, the internal type it uses, and, where one exists, a suggested supported replacement. Fix these before you move to the module path, because strong encapsulation blocks them at compile time and run time.
A few internal APIs, such as sun.misc.Unsafe, live in the jdk.unsupported module, which is still readable. Other APIs are completely hidden. jdeps tells you which case you are in.
Listing the modules an application needs
The --print-module-deps option prints a single comma-separated line of JDK modules, ready to paste into another command:
jdeps --print-module-deps --ignore-missing-deps --class-path 'libs/*' app.jarA typical result is java.base,java.logging,java.sql. --ignore-missing-deps stops jdeps from failing when some third-party classes cannot be resolved, which is common for optional dependencies.
Related options give different views:
--list-depslists the JDK modules and also package names that were not found.--list-reduced-depsomits modules that are already implied throughrequires transitive.
You can feed the result straight to jlink:
MODS=$(jdeps --print-module-deps --ignore-missing-deps --class-path 'libs/*' app.jar)
jlink --module-path "$JAVA_HOME/jmods" --add-modules "$MODS" --output imageGenerating module descriptors
Use --generate-module-info to draft a descriptor for a jar you cannot change:
jdeps --generate-module-info generated --module-path libs legacy-lib.jarjdeps writes generated/<module-name>/module-info.java, deriving the module name from the jar name. The draft contains requires lines for the JDK modules it found and exports lines for every package. Treat it as a starting point:
- Remove
exportsfor packages that are not public API. - Add
requires transitivewhere the library exposes another module's types in its signatures. - Add
usesandprovidesby hand, becausejdepscannot infer them reliably.
A variant, --generate-open-module, writes an open module descriptor, which is useful when the library depends on deep reflection.
What jdeps cannot see
Static analysis misses anything resolved at run time. These cases are the common gaps:
- Reflection.
Class.forName(name)with a non-constant string hides the target class. - Services.
ServiceLoader.load(X.class)shows a dependency on the interfaceX. It does not show which provider modules will be present. - Resources and configuration. Frameworks that read class names from XML or properties files are invisible.
Compensate for these by hand. For a jlink image, add provider modules with --add-modules, or use --bind-services, which pulls in every provider the resolved modules can use. Then run the application and watch for ClassNotFoundException, ServiceConfigurationError, or InaccessibleObjectException.
A migration checklist using jdeps
- Run
jdeps -son every jar to see the overall dependency graph. - Run
jdeps --jdk-internalsand replace the internal APIs it reports. - Add jars until no
not foundentries remain. - Generate draft descriptors for the jars you own, then tighten them by hand.
- Use
-verbose:classto find which classes cause package overlaps, then fix split packages. - Run the real application with
--module-pathand treat runtime errors as the final check.
Exercise
You run jdeps --print-module-deps --ignore-missing-deps app.jar and get java.base,java.logging. You build a jlink image with exactly those modules, and the app fails at startup with a ServiceConfigurationError for a JDBC driver loaded through ServiceLoader. Why did jdeps not report the driver module, and how do you fix the image?
Show answer
jdeps analyzes bytecode references only. The app asks ServiceLoader for a service interface, so the provider module is never named in the bytecode and is not reported. Fix the image by adding the provider module explicitly with --add-modules, or by passing --bind-services to jlink so every provider of the services used by the resolved modules is included. Then run the image to confirm the error is gone.