Week 113 — What is the jdeps tool and how can it be used?
Question of the Week #113
What is the jdeps tool and how can it be used?
4 Replies
jdeps
is a tool that can be used to list dependencies between packages and modules. It can be run on JARs and directories containing class files.
For example, assume a class Test
in a package a
:
This class uses the java.sql
module. After compiling it, it is possible to run the jdeps
command with the compiled classes folder as its first and only argument (jdeps path/to/compiled/class/folder
) and the output shows that code in the a
package (the Test
class) uses the java.lang
and java.sql
packages in their respective modules java.base
and java.sql
):
We can also add a class in another package:
and change the Test
class in the a
package to use that class:
When running jdeps
again on the directory containing the compiled class files, it shows that a
is using b
and that b
is accessing the packages java.lang
, java.io
and java.time
:
The
jdeps
command is also capable of generating a simple module-info.java
file requiring all dependenceis used by a JAR at compile-time. This is possible using the --generate-module-info
command followed by the directory the module-info.java
file should be generated in.
The following commands generate a JAR named project.jar
from the classes above (assuming they were compiled to directories a
and b
for the two packages) and generates a module-info.java
file for it in generated/project/module-info.java
The generated module-info.java
would look similar to the following:
📖 Sample answer from dan1st
Jdeps command shows the package level or class level dependencies of Java class files it is used to launch the Java class analyzee
Analyser
Submission from shambhavi2332