tags:

views:

156

answers:

3

Hello.

I am currently having the problem that I have a (partial) program that is trying to load a class but fails because it cannot find this class. Looking at the stack trace, I cannot see any particular reason for why the VM tries to load this particular class at the first place. Are there any tools that would let me figure out why a particular class is being loaded?

Hint: I am already getting a stack trace at the exact point where the JVM tries to load the class (through an agent). However, the stack trace contains no line numbers. Therefore I only know which method triggers the class being loaded, not which statement. Then, even knowing the statement may not be enough. A single statement can cause a class to be loaded in many ways, because sometimes the VM needs to load part of the transitive closure of classes.

+3  A: 

You might try a static analysis tool like JDepend to see what classes have references to that class.

Jeff Storey
I already did that but the dependencies are pretty subtle. The class that gets loaded is not mentioned in the method that is currently executing when the class is loaded. It must be something like a super class of a field type of a super class of a receiver class of some type in the current method... you get what I mean.
A: 

Classloaders

If it's an envorinment where multiple class-loaders are in the game (like a web application) you should be careful. Please tell us what's the app.

resource

Tell us where are the jars (you file/dir structure) and what class is loading. Are you loading it dinamically using Class.forName? or using spring or another framework of IOC? Is it the main class?

Some previous testing (to help us)

Maybe you can test some things using Class.getResource() from within the main method. If you class is foo.bar.Clazz try Class.getResource("/foo/bar/Clazz.class") to see if it returns something valid or not. Try to do the same with the class that loads your failing class to see if it's where you expect.

helios
I am already getting a stack trace at the exact point where the JVM tries to load the class (through an agent). However, the stack trace contains no line numbers. Therefore I only know which method triggers the class being loaded, not which statement.
My intention was to see if the class provoking the problematic class load was on the expected class loader (let's say jar) or another. That of course, if you have multiple class loaders... :(
helios
+16  A: 

Run your program with the -XX:+TraceClassLoading and -XX:+TraceClassResolution flags. This will create a LOT of output that looks like the following:

[Loaded com.kdgregory.example.memory.PermgenExhaustion$MyClassLoader from file:/home/kgregory/Workspace/Website/programming/examples/bin/]
RESOLVE com.kdgregory.example.memory.PermgenExhaustion$MyClassLoader java.net.URLClassLoader
RESOLVE java.net.URLClassLoader java.lang.Class URLClassLoader.java:188

You'll need to trace the chain of RESOLVE messages for a particular class. Or more likely, you'll see an error when your program attempts to load the class, preceeded by resolve messages for the class that loads it).

kdgregory
That looks useful. I will check this out, thanks!
Turned out to be just what I needed! Made my day! Thanks a lot!