Sub topic 9
Bootstrap, platform and application class loaders
This chapter covers the three built-in class loaders in Java 11, which classes each one loads, and how to inspect them. It assumes you can write and run basic Java programs, use javac and java, and understand static members and exceptions. Parent delegation, the rule that decides which loader gets asked first, is the next chapter's topic. Here we only look at who the loaders are and how they relate.
Why the JVM has more than one loader
A class loader is an object that turns a class name into a Class object by finding the bytes of a .class file and handing them to the JVM. The JVM does not use one loader for everything, for two reasons:
- Trust levels. Core classes such as
java.lang.Stringmust come from a trusted place. Application code on the class path must not be able to replace them. - Different sources. Classes come from the runtime image, from platform modules and from the application's class path. Each source is handled by a loader suited to it.
Every loaded class records which loader defined it. You can read that record with Class.getClassLoader(). As a later chapter shows, the defining loader is part of the class's identity.
The three built-in loaders
Java 9 introduced the module system and changed the loader layout. Java 8 had the bootstrap, extension and application loaders. In Java 9 and later, which includes Java 11, the extension loader is gone and a platform loader replaces it.
| Loader | Implemented in | Typical contents | `getClassLoader()` result |
|---|---|---|---|
| Bootstrap | The JVM itself (native code) | `java.base` and a few other core modules, such as `java.logging` and `java.xml` | `null` |
| Platform | Java (`jdk.internal.loader.ClassLoaders$PlatformClassLoader`) | Other Java SE and JDK modules, such as `java.sql` | The platform loader object |
| Application (system) | Java (`jdk.internal.loader.ClassLoaders$AppClassLoader`) | Your classes on the class path or module path, and JDK tool modules | The application loader object |
The exact set of modules the bootstrap loader handles depends on the JDK build. Do not hard-code assumptions about which module sits in which loader. Look at the result at run time instead.
Bootstrap loader
The bootstrap loader is part of the JVM and is written in native code, not in Java. It loads the most fundamental classes, including java.lang.Object and java.lang.String. Because it is not a Java object, there is nothing to represent it. Java code sees it as null.
This null is a frequent source of NullPointerException. Code such as clazz.getClassLoader().getResource(...) fails for core classes.
Platform loader
The platform loader loads the Java SE and JDK modules that are not handled by the bootstrap loader. You get it with ClassLoader.getPlatformClassLoader(), a method added in Java 9. It is a normal Java object, so you can print it and ask for its parent.
In Java 8 this role belonged to the extension loader, which read JAR files from the lib/ext directory. That mechanism, the java.ext.dirs property, was removed in Java 9. Code that depended on dropping JARs into lib/ext must put them on the class path instead.
Application loader
The application loader, also called the system class loader, loads classes from the class path (-cp, -classpath, or the CLASSPATH environment variable) and from the module path. The classes you write and the libraries you add normally end up here. Get it with ClassLoader.getSystemClassLoader(). A class loaded from the class path reports it from getClassLoader().
In Java 11 the application loader is not a URLClassLoader. In Java 8 you could cast it to URLClassLoader to read the class path. That cast throws ClassCastException in Java 9 and later. Read the java.class.path system property instead.
How the loaders relate
The platform loader's parent is the bootstrap loader, and the application loader's parent is the platform loader. The word "parent" here means the loader that gets asked first. It does not mean inheritance. Both PlatformClassLoader and AppClassLoader are Java classes, but the bootstrap loader is not.
The pointer returned by getParent() for the platform loader is null, again standing for bootstrap.
Observing the loaders
This program prints the loader for a core class, for the platform loader itself, and for the application loader with its parent.
public class LoaderDemo {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader());
System.out.println(ClassLoader.getPlatformClassLoader());
ClassLoader app = LoaderDemo.class.getClassLoader();
System.out.println(app);
System.out.println(app.getParent());
}
}On a typical Java 11 JDK the output looks like this. The @ hash values differ on every run.
null
jdk.internal.loader.ClassLoaders$PlatformClassLoader@1b6d3586
jdk.internal.loader.ClassLoaders$AppClassLoader@2c13da15
jdk.internal.loader.ClassLoaders$PlatformClassLoader@1b6d3586Reading line by line:
Stringwas loaded by the bootstrap loader, so the loader isnull.- The platform loader is a real object.
LoaderDemocame from the class path, so its loader is the application loader.- The application loader's parent is the same platform loader object as in line 2. The identical hash confirms it is a single instance.
Watching classes get loaded
Unified JVM logging, available since Java 9, records every class as it loads, along with where it came from.
$ java -Xlog:class+load:file=classload.log LoaderDemoOpen classload.log and you will see lines such as java.lang.Object source: jrt:/java.base for core classes and LoaderDemo source: file:/... for your own. The jrt: prefix names the runtime image. Classes read from the file system show a file: source. A line that says shared objects file means the class came from the class data sharing archive, a startup optimization that does not change which loader owns the class.
Walking the chain
You can write a small helper that prints the loader chain of any class. This is useful when debugging where a library was loaded from.
public class LoaderChain {
static void print(Class<?> c) {
ClassLoader loader = c.getClassLoader();
System.out.println(c.getName() + " is loaded by:");
if (loader == null) {
System.out.println(" bootstrap (null)");
return;
}
while (loader != null) {
System.out.println(" " + loader);
loader = loader.getParent();
}
System.out.println(" bootstrap (null)");
}
public static void main(String[] args) throws Exception {
print(String.class);
print(java.sql.Connection.class);
print(LoaderChain.class);
}
}The loop stops when getParent() returns null, so the bootstrap loader is printed by hand at the end. For java.sql.Connection you should see the platform loader followed by bootstrap. For LoaderChain you should see the application loader, the platform loader, and then bootstrap.
Loaders and the module system
Since Java 9, each named module is defined to exactly one loader. The JDK assigns its own modules to the bootstrap, platform or application loader when it builds the runtime image. You can see the result with Class.getModule():
public class ModuleOwner {
public static void main(String[] args) {
System.out.println(String.class.getModule());
System.out.println(java.sql.Connection.class.getModule());
System.out.println(ModuleOwner.class.getModule());
}
}The output is module java.base, module java.sql and unnamed module @.... Classes on the class path belong to the unnamed module of the application loader. They are not in any named module.
The loader that owns a module decides who can find its classes. This matters when a library on the class path tries to load a JDK-internal class. The module system may refuse access even though a loader could technically find the bytes.
Common mistakes
- Assuming
getClassLoader()never returnsnull. It returnsnullfor classes defined by the bootstrap loader. Check fornullbefore calling methods on the result. - Casting the application loader to
URLClassLoader. This worked in Java 8 and fails in Java 11. UseSystem.getProperty("java.class.path"). - Expecting a
lib/extdirectory to work. The extension mechanism no longer exists. - Using
Class.forNameorgetResourcewith the wrong loader.Thread.currentThread().getContextClassLoader()andFoo.class.getClassLoader()may return different loaders in frameworks and application servers. In a plainjavalaunch, both are the application loader. - Treating "parent" as a Java superclass.
AppClassLoaderdoes not extendPlatformClassLoader. The parent is a runtime link between loader objects.
Checking your understanding
Exercise
A program prints Foo.class.getClassLoader() and gets null. A colleague says this is a bug in the program. What is the correct explanation, and what should code that uses the result do?
Show answer
null means the bootstrap loader defined Foo. It is not a bug, because the bootstrap loader is native code with no Java object to represent it. Foo is therefore a core class, not one from the class path. Code that uses the result must check for null before calling methods on it, or use ClassLoader.getSystemClassLoader() as a fallback where appropriate.
Exercise
On Java 11, you run LoaderDemo from the class path. What are the values of LoaderDemo.class.getClassLoader().getParent() and LoaderDemo.class.getClassLoader().getParent().getParent()?
Show answer
The first is the platform loader (jdk.internal.loader.ClassLoaders$PlatformClassLoader). The second is null, which stands for the bootstrap loader.
Exercise
An older utility does URLClassLoader u = (URLClassLoader) ClassLoader.getSystemClassLoader(); to list the class path. It works on Java 8 and throws ClassCastException on Java 11. Why does it fail, and what is the replacement?
Show answer
In Java 9 and later the application loader is an internal class, AppClassLoader, that does not extend URLClassLoader. Read the class path with System.getProperty("java.class.path") instead.