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
kali
kali2w ago
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:
package a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Test{
public static void main(String[] args) throws SQLException{
Connection con = DriverManager.getConnection("jdbc:...");
// TODO
}
}
package a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Test{
public static void main(String[] args) throws SQLException{
Connection con = DriverManager.getConnection("jdbc:...");
// TODO
}
}
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):
. -> java.base
. -> java.sql
a -> java.lang java.base
a -> java.sql java.sql
. -> java.base
. -> java.sql
a -> java.lang java.base
a -> java.sql java.sql
We can also add a class in another package:
package b;

import java.time.LocalDate;

public class Util{
public static void displayCurrentDate(){
System.out.println(LocalDate.now());
}
}
package b;

import java.time.LocalDate;

public class Util{
public static void displayCurrentDate(){
System.out.println(LocalDate.now());
}
}
and change the Test class in the a package to use that class:
package a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import b.Util;

public class Test{
public static void main(String[] args) throws SQLException{
Connection con = DriverManager.getConnection("jdbc:...");
// TODO
Util.displayCurrentDate();
}
}
package a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import b.Util;

public class Test{
public static void main(String[] args) throws SQLException{
Connection con = DriverManager.getConnection("jdbc:...");
// TODO
Util.displayCurrentDate();
}
}
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:
. -> java.base
. -> java.sql
a -> b .
a -> java.lang java.base
a -> java.sql java.sql
b -> java.io java.base
b -> java.lang java.base
b -> java.time java.base
. -> java.base
. -> java.sql
a -> b .
a -> java.lang java.base
a -> java.sql java.sql
b -> java.io java.base
b -> java.lang java.base
b -> java.time java.base
kali
kali2w ago
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
jar cfe project.jar a.Test a b
jdeps --generate-module-info generated project.jar
jar cfe project.jar a.Test a b
jdeps --generate-module-info generated project.jar
The generated module-info.java would look similar to the following:
module project {
requires transitive java.sql;

exports a;
exports b;

}
module project {
requires transitive java.sql;

exports a;
exports b;

}
📖 Sample answer from dan1st
kali
kali2w ago
Jdeps command shows the package level or class level dependencies of Java class files it is used to launch the Java class analyzee
kali
kali2w ago
Analyser
Submission from shambhavi2332

Did you find this page helpful?