Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 681
paweloque Avatar asked Mar 15 '12 14:03

paweloque


2 Answers

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.

like image 182
sgp15 Avatar answered Sep 20 '22 01:09

sgp15


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

Obvious! (May NPE.)

(ClassLoaders can load classes from multiple locations.)

like image 28
Tom Hawtin - tackline Avatar answered Sep 22 '22 01:09

Tom Hawtin - tackline