Analyse the classpath of a running Java program

Sometimes we come across scenarios where it is required to analyze the classpath of a running java program to make sure that the correct libraries are picked up. In my scenario, I wanted to check whether an endorsed jar has been picked instead of the jar that comes with the JDK. Few of the possible ways of doing this is,

  • Use the ‘-verbose:class’ argument with the ‘java’ command when starting the program.
  • Use ‘System.getProperty(“java.class.path”);’ inside the program to print the classpath.

But both these issues have constraints, thus have a limited usability when it comes to third party application. But the following solution will be more elegant and it can be used while running the java application which might have been developed by us or some other party.

– First locate the process ID of the Java application. If you are on Linux this can be easily done using the system monitor or you can select the hard way, the terminal 😉

– Once you have identified PID, execute the command, ‘lsof -p <PID>’ (If you have a large number of dependent libraries, you can take the output of this command into a file by executing ‘lsof -p <PID> > <path/to/file>’ )

– This will generate the list of open files of that process. This contains the list of JARs, the program keeps in its classpath.



About this entry