Sub topic 9
Bootstrap, Platform, and Application Class Loaders
This chapter assumes you can compile and run basic Java programs and understand classes, methods, static members, and exceptions. You should also know how to use javac and java from a command line.
A class loader loads a class file into the JVM and creates a Class<?> object for it. Java 11 uses a hierarchy of class loaders:
- The bootstrap class loader loads core Java classes such as
java.lang.String. - The platform class loader loads Java platform modules that are not part of the core classes.
- The application class loader loads classes from the application class path and module path.
The bootstrap loader is implemented inside the JVM. It is not a normal Java object. Therefore, asking for the class loader of a bootstrap-loaded class returns null.
The platform and application loaders are Java objects. You can obtain them through ClassLoader methods or by asking a class for its loader.
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());
}
}A typical Java 11 run prints output similar to this:
null
jdk.internal.loader.ClassLoaders$PlatformClassLoader@...
jdk.internal.loader.ClassLoaders$AppClassLoader@...
jdk.internal.loader.ClassLoaders$PlatformClassLoader@...The exact object addresses and class names can vary. The important relationships are:
bootstrap
parent of platform
parent of applicationThe application loader is also called the system class loader. It normally finds classes in the class path, such as the LoaderDemo class in the example.
Parent delegation
When a loader is asked for a class, it normally asks its parent first. Only when the parent cannot find the class does the child try to find it itself. This behavior is called parent delegation.
Parent delegation protects the identity of core classes. For example, an application cannot normally replace java.lang.String with a class of its own. The bootstrap loader gets the first opportunity to provide that class.
The delegation direction can seem surprising. The application loader depends on the platform loader, and the platform loader depends on the bootstrap loader. A request moves upward first, then a successful result moves back down to the requesting loader.
You can observe class loading with Java 11's unified logging:
java -Xlog:class+load:file=classload.log LoaderDemoThe log records loaded classes and their sources. This helps identify whether a class came from the bootstrap modules, the platform modules, or the application class path.
Common mistakes
- Treating the bootstrap loader as an ordinary
ClassLoaderobject. Its reference is represented bynull. - Assuming every class on the class path is loaded immediately. Classes are generally loaded when the JVM needs them.
- Assuming the application loader can override platform classes. Parent delegation normally prevents this.
- Comparing only class names when debugging type errors. The defining loader matters too.
Exercise
What does String.class.getClassLoader() usually return, and why?
Show answer
It returns null because String is loaded by the bootstrap class loader, which is implemented inside the JVM rather than represented as a normal Java ClassLoader object.