Java verbose class loading

JavaJvmClassloader

Java Problem Overview


I am trying to list the order in which the Java class loader is loading my classes. if I use -verbose parameter it will list every single interface/class it loads, including tons of interfaces such as Serializable, exceptions etc. Is there a way to tweak this output so it only shows which classes are loaded in the class my main method is defined?

Java Solutions


Solution 1 - Java

I guess your best bet is to do the following:

  • Output some fixed text once your main method starts and right before it ends.
  • Pipe the verbose output into a file
  • Use things like less or grep to find the classes loaded between the two tags from the main method.

There's a similar question and some answers here: https://stackoverflow.com/questions/1432180/is-there-a-way-to-get-which-classes-a-classloader-has-loaded

Did you try -verbose:class?

Solution 2 - Java

Here's a sed expression that will parse the output of java -verbose:class to produce pairs of loaded class name and its jar file. You can further pipe through a sort to get unique jar files. For example,

java -verbose:class -version 2>/dev/null |
  sed -ne 's/\[Loaded \(.\+\) from \(.\+\)\]/\2/p' | 
  sort -u

outputs

/usr/local/jdk1.7.0_67/jre/lib/rt.jar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBober02View Question on Stackoverflow
Solution 1 - JavanwinklerView Answer on Stackoverflow
Solution 2 - JavaNicholas SushkinView Answer on Stackoverflow