Running jdeps on your code

The jdeps class dependency analysis tool is not new to Java but perhaps has never been as important to developers than with the advent of Java's modular system. An important step to migrating your applications to Java 9, 10, or 11 is to run the jdeps tool to determine the dependencies your applications and its libraries have. The jdeps tool does a great job of suggesting replacements if your code has dependencies on any internal APIs.

The following screenshot shows the options available to you when using the jdeps analyzer:

When you use the jdeps -help command, you will also see module dependent analysis options, options to filter dependencies and options to filter classes to be analyzed.

Let's take a look at an example. Here is a simple Java class called DependencyTest:

import sun.misc.BASE64Encoder; 

public class DependencyTest {
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
BASE64Encoder.class.newInstance();
System.out.println("This Java app ran successfully.");
}
}

Now let's use javac to compile this class using Java 8:

As you can see, Java 8 successfully compiled the class and the application ran. The compiler did give us a DependencyTest.java:6: warning: BASE64Encoder is internal proprietary API and may be removed in a future release warning. Now let's see what happens when we try to compile this class using Java 9:

In this case, with Java 9, the compiler gave us two warnings instead of one. The first warning is for the import sun.misc.BASE64Encoder; statement and the second for the BASE64Encoder.class.newInstance(); method call. As you can see, these are just warnings and not errors, so the DependencyTest.java class file is successfully compiled.

Next, let's run the application:

Now we can clearly see that Java 9 will not allow us to run the application. Next, let's run a dependency test using the jdeps analyzer tool. We will use the following command-line syntax—jdeps DependencyTest.class:

As you can see, we have three dependencies: java.io, java.lang, and sun.misc. Here we are given the suggestion to replace our sun.misc dependency with rt.jar.

For a final test, we will try to compile DependencyTest using Java 10:

Here, we see that we simply cannot compile the application. Both JDK 10 and 11 provide the same error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset