The java.lang.ClassLoader class is the part of the Java runtime that finds the bytes of a class, loads them into the JVM and turns them into a Class object. Every class in a running program was loaded by some class loader, and the loader also defines the namespace the class lives in, so it governs visibility, isolation and reloading.
You rarely call it directly in ordinary code, but application servers, plugin systems, frameworks and tools such as Spring, Tomcat and Maven depend on it.
When your code first uses a class, the JVM asks a class loader to load it. The loader locates the binary form (a .class file in a directory, a JAR, a module or even a network stream), reads the bytes and calls defineClass to create the Class object. Linking and initialisation then follow.
A class is identified in the JVM by its fully qualified name and the loader that defined it. The same .class file loaded by two different loaders produces two distinct, incompatible types, which is the cause of confusing ClassCastException messages such as "class X cannot be cast to class X".
Since Java 9 and the module system, the JVM has three built-in loaders in a parent chain:
| Loader | getName() | Loads |
|---|---|---|
| Bootstrap | null (represented as null) | Core modules such as java.base |
| Platform | platform | Other platform modules, such as java.sql |
| Application (system) | app | Classes from the class path and module path |
Before Java 9 the middle level was the extension class loader, which used the ext directory. That mechanism was removed, and the platform loader replaced it. The bootstrap loader is implemented inside the JVM, which is why String.class.getClassLoader() returns null.
By default a loader delegates to its parent before trying itself. loadClass(name) follows this order:
findLoadedClass).ClassNotFoundException, call findClass to search locally.This guarantees that core classes such as java.lang.String always come from the bootstrap loader, so application code cannot replace them with a malicious version. It also means each class is loaded once per hierarchy. Web containers deliberately bend the rule so each web application prefers its own libraries.
To load classes from a custom source, extend ClassLoader and override findClass, not loadClass, so that delegation still works.
Usage:
For loading from JARs, prefer java.net.URLClassLoader, which already implements this. It is Closeable, so use try-with-resources to release the file handles when you are finished.
getResource and getResourceAsStream use the same delegation to find files on the class path.Class.forName(name, true, loader).For resources, prefer SomeClass.class.getResourceAsStream or the thread context class loader, which frameworks use when library code needs to see application classes.
| Symptom | Likely cause |
|---|---|
ClassNotFoundException | The loader cannot find the class on its search path |
NoClassDefFoundError | The class was present at compile time but failed to load or initialise at runtime |
ClassCastException between same-named classes | Two loaders each loaded their own copy |
| Memory leak after redeploy | A static reference or thread keeps the old loader alive |
LinkageError | The same class was defined twice in one loader |
A class can be unloaded only when its defining loader becomes unreachable, which is why leaked references to a web application's loader cause metaspace growth. Since Java 7 loaders that are parallel capable (registerAsParallelCapable) lock per class name rather than per loader, which helps concurrency.
ClassNotFoundException is a checked exception thrown when code asks a loader for a class by name and it is not found. NoClassDefFoundError is an error thrown when a class that was available at compile time cannot be found or initialised at runtime.
The bootstrap class loader is built into the JVM and has no Java object, so the API represents it as null.
Yes, by different loaders. Each copy is a separate type, so instances of one are not assignable to the other.
Override findClass. Overriding loadClass risks breaking parent delegation.
ClassLoader turns class bytes into Class objects and defines which classes a part of your program can see. Java uses a bootstrap, platform and application loader with parent delegation. Extend ClassLoader through findClass or use URLClassLoader for plugins, and watch for duplicate-class and leak problems when several loaders are involved.