paweloque
paweloque

Reputation: 18894

Find the ClassLoader loading a specific class

Is there a way to determine which ClassLoader loads a specific class? Or more specifically from where a specific class is loaded?

I've a situation where an old db driver class is loaded. I'd like to find the file where the old driver is loaded from.

My initial approach is to set a debug point on the ClassLoader.loadClass(..) method and stop the vm once the class is getting loaded to see which classloader is loading it. Unfortunately the loadClass method is called so often that its difficult to stop where the class is loaded. I'll try to set a breakpoint filter. There is, however, another problem: because of the ClassLoader architecture the loadClass is called even if the ClassLoader is not responsible for loading.

There must be a better way to achieve what I want. Do you have an idea or suggestion where to look for a solution?

Upvotes: 6

Views: 3101

Answers (3)

joe
joe

Reputation: 349

Also the -verbose:jni option of java command will show the class loaded which are NOT written in java, ie "java native interface".

-dbednar

Upvotes: 0

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

clazz.getProtectionDomain().getCodeSource().getLocation()

Obvious! (May NPE.)

(ClassLoaders can load classes from multiple locations.)

Upvotes: 6

sgp15
sgp15

Reputation: 1280

How do you launch your program?

Adding following option to command line logs location of every class being loaded.

-verbose:class

These logs typically appear in sysout. But depending on how logging is configured you may have to look around a little.

Upvotes: 7

Related Questions