Finding out the classloader that loaded a particular class

You can have your classes loaded by several distinct classloaders. For example, you may have a URLClassLoader that loads your classes over the network.

To determine what ClassLoader loaded a particular class, you can invoke getClassLoader on the Class object of that class. This method returns null if the default system classloader was used to load the class.

Main.java:

public class Main {
   public static void main(String args[]) throws Exception {
 
      System.out.println(Main.class.getClassLoader());
      System.out.println(A.class.getClassLoader());
      System.out.println(String.class.getClassLoader());
   }
}
 
class A 
{
}

outputs:

sun.misc.Launcher$AppClassLoader@81012801
sun.misc.Launcher$AppClassLoader@81012801
null