Accessing the ClassLoader of a Class

This section provides a tutorial example on how to access the ClassLoader of a given class using the Class.getClassLoader() method.

If you want to access the ClassLoader object of a given class object, you can use the following logic:

   Class cls = obj.getClass();
   ClassLoader loader = cls.getClassLoader();

If you want to access the ClassLoader object of a given class name, you can use the following logic:

   Class cls = Class.forName(className);
   ClassLoader loader = cls.getClassLoader();

Here is tutorial example program on how to example JVM built-in ClassLoaders and access the ClassLoader of a given class:

class is wrong...

/* ClassLoaderTest.java
 * Copyright (c) HerongYang.com, All Rights Reserved.
 */
class ClassLoaderTest {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      Object o = null;
      Class c = null;
      ClassLoader l = null;

      l = ClassLoader.getSystemClassLoader();
      out.println("");
      out.println("Built-in ClassLoaders");
      out.println("System ClassLoader: "+l.getClass().getName());
      l = l.getParent();
      out.println("Extensions ClassLoader: "+l.getClass().getName());
      l = l.getParent();
      out.println("Bootstrap ClassLoader: "+l);

      o = new java.lang.String();
      c = o.getClass();
      l = c.getClassLoader();
      out.println("");
      out.println("ClassLoader of java.lang.String: "+l);

      try {
         c = Class.forName("sun.security.pkcs11.P11Util");
         l = c.getClassLoader();
         out.println("");
         out.println("ClassLoader of sun.security.pkcs11.P11Util: "
            +l.getClass().getName());
      } catch (Exception e) {
      }

      o = new ClassLoaderTest();
      c = o.getClass();
      l = c.getClassLoader();
      out.println("");
      out.println("ClassLoader of ClassLoaderTest: "
         +l.getClass().getName());
   }
}

When executed with the JVM in JDK 10, I got this result:

C:\herong>java ClassLoaderTest

Built-in ClassLoaders
System ClassLoader: jdk.internal.loader.ClassLoaders$AppClassLoader
Extensions ClassLoader: 
   jdk.internal.loader.ClassLoaders$PlatformClassLoader
Bootstrap ClassLoader: null

ClassLoader of java.lang.String: null

ClassLoader of sun.security.pkcs11.P11Util: 
   jdk.internal.loader.ClassLoaders$PlatformClassLoader

ClassLoader of ClassLoaderTest: 
   jdk.internal.loader.ClassLoaders$AppClassLoader

The test result confirms that:

Comparing with JDK previous versions, HotSpot JVM has changed class loarders from sun.misc.Launcher.* to jdk.internal.loader.*. Hers is the result from JDK 1.8, or JDK 1.7:

C:\herong>java ClassLoaderTest

Built-in ClassLoaders
System ClassLoader: sun.misc.Launcher$AppClassLoader
Extensions ClassLoader: sun.misc.Launcher$ExtClassLoader
Bootstrap ClassLoader: null

ClassLoader of java.lang.String: null

ClassLoader of sun.security.pkcs11.P11Util:
   sun.misc.Launcher$ExtClassLoader

ClassLoader of ClassLoaderTest: sun.misc.Launcher$AppClassLoader

Last update: 2018.

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

ClassLoader Class - Class Loaders

 What Is Class Loader

 What Is java.lang.ClassLoader Class

Accessing the ClassLoader of a Class

 JVM "-verbose:class" Option

 loadClass() Method - Loading Classes Explicitly

 getSystemResource() Method - Finding Files

 Class Loading Problem - JAR Hell

 ClassChecker.java - Reports Class Loader

 ClassChecker.java - Reports Class Locations

 "superclass access check failed" Class Load Error

 Class Loading Followed by Class Initialization

 Class Class - Class Reflections

 JVM Runtime Data Areas

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 JRockit JVM 28.2.7 by Oracle Corporation

 Outdated Tutorials

 References

 Full Version in PDF/EPUB