Find ClassLoader to load a specific class

Is there a way to determine which ClassLoader loads a particular class? Or more specifically, where does a particular class come from?

I have a situation where the old db driver class is loaded. I would like to find the file from which the old driver was loaded.

My initial approach is to set the debugging point of the ClassLoader.loadClass (..) method and stop vm as soon as the class loads, to see which class loader loads. Unfortunately, the loadClass method is called so often that it is difficult to stop where the class is loaded. I will try to set a breakpoint filter. However, there is another problem: because of the ClassLoader architecture, loadClass is called even if 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?

+4
source share
3 answers

How do you run your program?

Adds the following parameter to the log location on the command line of each loadable class.

-verbose:class 

These logs usually appear in sysout. But depending on how the registration is configured, you may need to look around a bit.

+6
source
 clazz.getProtectionDomain().getCodeSource().getLocation() 

The obvious! (May NPE.)

( ClassLoader can load classes from multiple locations.)

+4
source

Also, the -verbose: jni option of the java command will show a loaded class that is NOT written to java, that is, a "native Java interface".

-dbednar

0
source

Source: https://habr.com/ru/post/1401613/


All Articles