Search code examples
javaruntime-errorclassloader.class-file

Determine path of class file that failed to load


We get an UnsupportedClassVersionError but have problems identifying the location of the offending file (>15 JARs in the classpath, several of which may contain the particular class). We suspect that the same class may be in the class path multiple times.

Java option -verbose:class prints exactly what we want, but only for successfully loaded classes. The class (file) that causes the error is not printed.

How can we determine the physical location of the class file (most likely in a JAR) that causes the class loader error?

I thought it might be possible to set a breakpoint in ClassLoader but all the relevant methods are native.


Solution

  • Java's classloaders that rely on the class path at all search it in order, and use (or try to) the first definition they find for any given class they are trying to load. In particular, it does not matter which class Java first discovers to require your problem class. These class loaders perform the same path search for every class they try to load.

    Very likely the class name -- especially its package -- will give you an idea of which jars to check, but you don't really need that. The simplest and most reliable solution is to do as Java does: check each jar, in the order it appears in the class path.

    You can list the contents of a jar with jar -tf myjar.jar. If you are looking for org.other.SomeClass, it would appear in the jar as /org/other/SomeClass.class.