
Getting ready
We need a sample application that we can run against the jdeps command to find its dependencies. So, we thought of creating a very simple application that uses the Jackson API to consume JSON from the REST API: http://jsonplaceholder.typicode.com/users.
In the sample code, we also added a call to the deprecated JDK internal API, called sun.reflect.Reflection.getCallerClass(). This way, we can see how jdeps helps in finding dependencies for the JDK internal APIs.
The following steps will help you to set up the prerequisites for this recipe:
- You can get the complete code for the sample from Chapter03/1_json-jackson-sample. We have built this code against Java 9 and also using Java 8, and it compiles well. So, you only need to install Java 9 to compile it. If you try to compile with JDK 11, you will face an error due to the deprecated internal API, which is no longer available.
- Once you have the code, compile it by using the following:
# On Linux
javac -cp 'lib/*' -d classes
-sourcepath src $(find src -name *.java)
# On Windows
javac -cp lib*;classes
-d classes src/com/packt/model/*.java
src/com/packt/*.java
# On Linux
"$JAVA8_HOME"/bin/javac -cp 'lib/*'
-d classes -sourcepath src $(find src -name *.java)
# On Windows
"%JAVA8_HOME%"\bin\javac -cp lib\*;classes
-d classes src\com\packt\*.java
src\com\packt\model\*.java
You will see a warning for the use of an internal API, which you can safely ignore. We added this with a purpose to demonstrate the capability of jdeps. Now, you should have your compiled class files in the classes directory.
- You can create an executable JAR and run the sample program by running the JAR using the following commands:
# On Linux:
jar cvfm sample.jar manifest.mf -C classes .
"$JAVA8_HOME"/bin/java -jar sample.jar
# On Windows:
jar cvfm sample.jar manifest.mf -C classes .
"%JAVA8_HOME%"\bin\java -jar sample.jar
- We have provided the run.bat and run.sh scripts in Chapter03/1_json-jackson-sample. You can compile and run using these scripts as well.
A sample.jar file gets created in the current directory if you have used run.bat or run.sh or the preceding commands to create JAR. If the JAR hasn't been created, you can use the build-jar.bat or build.-jar.sh script to compile and build the JAR.
So, we have a sample non-modular application that we will analyze using jdeps to find its dependencies, and also the names of the modules it possibly depends on.