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:

/**
 * ClassLoaderTest.java
 * Copyright (c) 2010, 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 for java.lang.String: "+l);

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

When executed on my Windows 7 system with JDK 1.7.0, I got this result:

C:\herong\jvm>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

The test result confirms that:

Comparing to the same test with JDK 1.6.0 in 2010, the behavior of getSystemClassLoader() is not changed.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 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

 Outdated Tutorials

 References

 PDF Printing Version