Tuesday, March 25, 2014

jdeps: JDK 8 Command-line Static Dependency Checker

I'm a big fan of the command-line tools delivered with the Sun/Oracle HotSpot JDK. I have blogged about some of my favorites including jcmd, jps, jstack, javap, javac (-Xlint and -Xprint), jinfo, and xjc. In this post, I look briefly at a new command-line tool introduced with JDK 8: jdeps.

Although there is understandable and significant disappointment that we did not get modularity built into the Java platform with the punting of Project Jigsaw to a future release of Java, we can still gain benefits in JDK 8 from some work that went into getting the platform ready for Project Jigsaw. The new jdeps command came from work on Java modularity and is delivered with JDK 8.

jdeps is a "Java [static] class dependency analyzer" that is useful for quickly identifying "static dependencies of [developers'] applications and libraries." The level of detail shown is configurable and some ways of configuring this command's output are shown next.

The following screen snapshot demonstrates basic usage of the jdeps command against a Guava 16 JAR. Note that the basic use indicates dependencies at the package level (which packages in the JAR depend on which other packages).

Much (and I emphasize the "much") greater detail is gained by using jdeps -v (verbose) because this reports class-level dependencies rather than only package-level dependencies. The beginning of such output is shown next.

The class-level output becomes very verbose quickly and so it will often need to be redirected to a file or one can use jdeps's -dotoutput argument to write the output into files in the directory specified with the -dotoutput argument. The latter is demonstrated in the next screen snapshot.

As the screen snapshot above demonstrates, executing jdeps with the -dotoutput argument writes *.dot files to the specified directory. One of the files has the same name as the JAR whose dependencies are being analyzed (but with .dot extension rather than .jar extension) and the other file is a summary of static dependencies (much shorter than the verbose file in the same directory). These .dot files are text files and can be viewed with any text editor or text display command. As a side note, machines running Windows may "recognize" these files as Word template files based on their .dot extension (see next screen snapshot), but these are simple text files.

Conclusion

There are a few more useful options available for use with jdeps that are not covered in this post, but these can be seen by running jdeps -help or by accessing the tool's Tech Notes. The jdeps command-line tool is easy to use and quickly identifies static dependencies on a provided JAR and its package or class contents.

No comments: