JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
ClassChecker.java - Reports Class Loader
This section provides a tutorial example, ClassChecker.java, which reports the class loader by the JVM to load a given class.
While testing class loading behavior of the HotSpot JVM, I have created a simple class loading tool called ClassChecker.java:
/* ClassChecker.java * Copyright (c) HerongYang.com. All Rights Reserved. */ class ClassChecker { public static void main(String[] a) { if (a.length<1) { System.out.println("Missing class name. Usage example:"); System.out.println(" java ClassChecker java.net.URL"); return; } String className = a[0]; String resourceName = className.replaceAll("\\.", "/")+".class"; try { Class clazz = Class.forName(className); ClassLoader loader = clazz.getClassLoader(); System.out.println("Class: "+clazz.getName()); System.out.println("Loader: "+loader); java.net.URL url = ClassLoader.getSystemResource(resourceName); System.out.println("Loaded from: "+url); System.out.println("All locations: "); java.util.Enumeration e = ClassLoader.getSystemResources(resourceName); while (e.hasMoreElements()) { url = (java.net.URL) e.nextElement(); System.out.println(" "+url); } } catch (Exception e) { e.printStackTrace(); } } }
ClassChecker.java can help you to identify which class loader is used by the JVM to load your class. For example:
herong> java ClassChecker ClassChecker Class: ClassChecker Loader: jdk.internal.loader.ClassLoaders$AppClassLoader@629f0666 Loaded from: file:/C:/herong/ClassChecker.class All locations: file:/C:/herong/ClassChecker.class
The output tells me that class "ClassChecker" is loaded by the jdk.internal.loader.ClassLoaders$AppClassLoader class loader.
If check the sun.security.pkcs11.P11Util class, it tells me that jdk.internal.loader.ClassLoaders$PlatformClassLoader is used:
herong> java ClassChecker sun.security.pkcs11.P11Util Class: sun.security.pkcs11.P11Util Loader: jdk.internal.loader.ClassLoaders$PlatformClassLoader@47fd17e3 Loaded from: jrt:/jdk.crypto.cryptoki/sun/security/pkcs11/P11Util.class All locations: jrt:/jdk.crypto.cryptoki/sun/security/pkcs11/P11Util.class
Table of Contents
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 java.lang.ClassLoader Class
Accessing the ClassLoader of a Class
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 Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
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