Where did this class come from?

How would you begin to establish where the class (or possibly the resource) was loaded?

I'm trying to figure out exactly where the class was loaded. Does anyone know if you can find out the following:

  • In which Jar file did the class appear?
  • Which classloader downloaded the file?
+3
source share
6 answers

The class Classhas an instance method getClassLoader()that returns a reference to the class loader, which loads the class that it represents. Please note that this may return null. See here .

So, if you want to know which classloader is loaded String(as an example), you can do:

ClassLoader loader = String.class.getClassLoader();

or

ClassLoader loader = "I'm a String".getClass().getClassLoader();
+5

URL-, , , MyClass.class.getResource( "MyClass.class" ).

+2

MyClass.class.getProtectionDomain().getCodeSource().getLocation(). ( , null ProtectionDomain hava a CodeSource. My SecurityException.

+2

, -verbose:class, , grep ( DOS find, ), , .

0

if you have an instance of the insta.getClass () class. getClassLoader (). getName () -> this is the class loader loading the class

insta.getClass (). getProtectionDomain (). getCodeSource (). getLocation () - where the class was the loader from

0
source

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


All Articles