How to explore which classes are loaded from which JARs?

cwash picture cwash · Jun 3, 2009 · Viewed 29.1k times · Source

Is there a way to determine which classes are loaded from which jars at runtime?

I'm sure we've all been in JAR hell before. I've run across this problem a lot troubleshooting ClassNotFoundExceptions and NoClassDefFoundErrors on projects. I'd like to avoid finding all instances of a class in jars and using process of elimination on the code causing a CNFE to find the culprit.

Will any profiling or management tools give you this kind of information?

This problem is super annoying purely because we should have this information at the time the class gets loaded. There has to be a way to get to it, or record it and find it, yet I know of nothing that will do this, do you?

I know OSGi and versioned bundles/modules aim to make this a non issue... but it doesnt seem to be going away any time soon. :)

NOTE: I found this question is a subset of my question related to classes loaded from versioned jars.

Update: Somewhat related, this post explains a strategy to search for a class within jars (either under the current directory) or in your M2_REPO. JarScan, scan all JAR files in all subfolders for specific class

Update 2: Also somewhat related, JBoss Tattletale

Answer

Jason Day picture Jason Day · Jun 3, 2009

Passing the -verbose:class switch to the java command will print each class loaded and where it was loaded from.

Joops is also a nice tool for finding missing classes ahead of time.