
How to do it...
- The simplest way to use jdeps is as follows:
# On Linux
jdeps -cp classes/:lib/* classes/com/packt/Sample.class
# On Windows
jdeps -cp "classes/;lib/*" classes/com/packt/Sample.class
The preceding command is equivalent to the following:
# On Linux
jdeps -verbose:package -cp classes/:lib/*
classes/com/packt/Sample.class
# On Windows
jdeps -verbose:package -cp "classes/;lib/*" classes/com/packt/Sample.class
The output for the preceding code is as follows:

In the preceding command, we use jdeps to list the dependencies for the class file, Sample.class, at the package level. We have to provide jdeps with the path to search for the dependencies of the code being analyzed. This can be done by setting the -classpath, -cp, or --class-path option of the jdeps command.
- Let's list the dependencies at the class level:
# On Linux
jdeps -verbose:class -cp classes/:lib/*
classes/com/packt/Sample.class
# On Windows
jdeps -verbose:class -cp "classes/;lib/*" classes/com/packt/Sample.class
The output of the preceding command is as follows:

In this case, we make use of the -verbose:class option to list the dependencies at the class level, which is why you can see that the com.packt.Sample class depends on com.packt.model.Company, java.lang.Exception, com.fasterxml.jackson.core.type.TypeReference, and so on.
- Let's get the summary of the dependencies:
# On Linux
jdeps -summary -cp classes/:lib/*
classes/com/packt/Sample.class
# On Windows
jdeps -summary -cp "classes/;lib/*"
classes/com/packt/Sample.class
The output is as follows:

- Let's check for the dependency on the JDK internal API:
# On Linux
jdeps -jdkinternals -cp classes/:lib/*
classes/com/packt/Sample.class
# On Windows
jdeps -jdkinternals -cp "classes/;lib/*"
classes/com/packt/Sample.class
The following is the output of the preceding command:

- Let's run jdeps on the JAR file, sample.jar:
# On Linux and Windows
jdeps -s -cp lib/* sample.jar
The output we get is the following:

The preceding information obtained after investigating the sample.jar using jdeps is quite useful. It clearly states the dependencies of our JAR files and is very useful when we try to migrate this application to a modular application.
- Let's find whether there are any dependencies on a given package name:
# On Linux and Windows
jdeps -p java.util sample.jar
The output is as follows:

The -p option is used to find dependencies on the given package name. So, we get to know that our code depends on the java.util package. Let's try this with another package name:
jdeps -p java.util.concurrent sample.jar
There is no output, which means that our code doesn't depend on the java.util.concurrent package.
- We would want to run the dependency check only for our code. Yes, this is possible. Suppose we run jdeps -cp lib/* sample.jar; you will see even the library JARs being analyzed. We wouldn't want that, right? Let's just include the classes of the com.packt package:
# On Linux
jdeps -include 'com.packt.*' -cp lib/* sample.jar
# On Windows
jdeps -include "com.packt.*" -cp lib/* sample.jar
The output is as follows:

- Let's check whether our code is dependent on a specific package:
# On Linux
jdeps -p 'com.packt.model' sample.jar
# On Windows
jdeps -p "com.packt.model" sample.jar
The output is as follows:
- We can use jdeps to analyze the JDK modules. Let's pick the java.httpclient module for analysis:
jdeps -m java.xml
Here is the output:

We can also find out whether a given module is dependent on another module by using the --require option, as follows:
# On Linux and Windows
jdeps --require java.logging -m java.sql
Here is the output:

In the preceding command, we tried to find out whether the java.sql module is dependent on the java.logging module. The output we get is the dependency summary of the java.sql module and the packages in the java.sql module, which make use of the code from the java.logging module.