Sub topic 17
Analyzing dependencies with jdeps
Assume you know how to compile Java programs, create JAR files, and use the module path. This chapter focuses on jdeps, the JDK tool that analyzes compiled classes and reports their dependencies.
What `jdeps` analyzes
jdeps performs static analysis. It reads class files and identifies references to packages, classes, JAR files, and modules. It does not run your application.
This makes it useful when you want to:
- Find which JDK modules an application needs.
- Detect dependencies on internal JDK APIs.
- Understand dependencies between JAR files.
- Prepare an application for JPMS migration.
- Select modules for a
jlinkruntime image.
The simplest command analyzes a JAR:
jdeps app.jarThe output lists the analyzed archive, the packages it uses, and the packages or modules that provide them.
Finding required JDK modules
For modular deployment, use --print-module-deps:
jdeps --print-module-deps app.jarA result might look like this:
java.base,java.sqlThe result is a comma-separated list suitable for jlink:
jlink \
--module-path "$JAVA_HOME/jmods" \
--add-modules java.base,java.sql \
--output imageIf the application contains several JAR files, analyze all of them:
jdeps --print-module-deps \
--class-path "libs/*" \
app.jarUse --recursive to follow dependencies through referenced classes and JAR files:
jdeps --recursive --print-module-deps app.jarWithout --recursive, the report can omit dependencies that are reached indirectly.
Reading the result correctly
jdeps reports references visible in bytecode. It cannot reliably discover dependencies created dynamically through reflection, configuration files, generated code, or service loading.
For example, this code hides the implementation class from ordinary static analysis:
Class<?> type = Class.forName("com.example.Plugin");
Object plugin = type.getDeclaredConstructor().newInstance();You must inspect configuration and runtime behavior separately. A successful jdeps command does not prove that the application will start correctly in a reduced runtime image.
Detecting internal API usage
Use --jdk-internals to find references to JDK-internal APIs:
jdeps --jdk-internals app.jarSuch APIs are outside the supported Java SE API. Replace them with public alternatives before modularizing when possible.
You can also ask jdeps to check a module descriptor:
jdeps --check com.example.app \
--module-path outThis can reveal unused qualified exports, concealed packages, and problems in the module graph.
A dependency analysis workflow
Run the analysis again after changing dependencies or upgrading the JDK. Compare the module list with the modules declared in module-info.java.
Exercise
Why might an application still fail in a jlink image even when jdeps reports the expected modules?
Show answer
jdeps may not detect dependencies created by reflection, configuration, generated code, or service loading. Those dependencies must be examined and included separately.