简体   繁体   中英

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?

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.

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

Obvious! (May NPE.)

( ClassLoader s can load classes from multiple locations.)

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

-dbednar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM