Java Reference ยท Language Features by Version
Java class loading
How the JVM finds, loads, links and initializes classes: the boot, platform and application class loaders, parent delegation and custom loaders.
A Java program is not one big executable. It is a set of classes, and the JVM brings each class into memory the first time the program needs it. The component that finds the bytes of a class (usually a .class file or an entry in a JAR) and turns them into a Class object is a class loader. Understanding loaders explains ClassNotFoundException, NoClassDefFoundError, why two copies of "the same" class can be incompatible, and how frameworks load plugins.
Loading is lazy. Mentioning a class in your source does not load it; running code that actually uses it does. Every class goes through three phases: loading (read the bytes and create the Class object), linking (verify the bytecode, prepare static fields with default values, resolve references) and initialization (run static initializers and assign static field values).
ClassLoader loader = Main.class.getClassLoader(); Class<?> c = Class.forName("java.util.ArrayList"); // load + initialize Class<?> d = loader.loadClass("java.util.ArrayList"); // load only
The built-in loaders
| Loader | getName() |
Loads |
|---|---|---|
| Boot loader | none (null when you ask a class for its loader) |
Core modules such as java.base, so String.class.getClassLoader() is null |
| Platform loader | platform |
Other JDK modules, for example java.sql |
| Application (system) loader | app |
Classes on the class path or module path of your program |
A loader asks its parent first (parent delegation). Only if the parent cannot find the class does the loader try itself. This guarantees that java.lang.String always comes from the JDK, so an application cannot replace it with its own version.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
How delegation works
ClassLoader.loadClass(name) follows a fixed recipe:
- Check whether this loader has already loaded the class (
findLoadedClass). - Ask the parent loader, or the boot loader if there is no parent.
- If the parent fails, call
findClass(name), which is the method a custom loader overrides. - If
findClassthrowsClassNotFoundException, the lookup fails.
A custom loader should override findClass, not loadClass, unless it deliberately breaks delegation (as web servers do so each web application can carry its own library versions). Inside findClass you read the bytes from wherever they live and call defineClass(name, bytes, 0, bytes.length).
Usage notes
- Class identity is loader plus name. The same bytes loaded by two loaders give two distinct
Classobjects. Casting between them throwsClassCastExceptioneven though the printed names are identical. Plugin systems and application servers hit this regularly. ClassNotFoundExceptionvsNoClassDefFoundError. The first (a checked exception) is thrown when you ask for a name by string, for exampleClass.forName("x.Y"). The second (anError) means a class was present when your code was compiled but is missing or failed to initialize at run time.ExceptionInInitializerErrorwraps an exception thrown by a static initializer. After that, further uses of the class throwNoClassDefFoundError, because the class stays in a failed state.- Initialization is thread-safe. The JVM guarantees that a class's static initializer runs once, even when many threads use the class at the same time. This is why the "holder idiom" works for lazy singletons.
- Compile-time constants (
static finalprimitives or strings initialized with constant expressions) are copied into the code that uses them, so reading them does not initialize their class. - Unloading. A class can be garbage collected only when its loader is unreachable. This is why loaders are leak-prone in long-running servers: one stray reference to a class from a thread or a static cache keeps a whole old web application alive.
- Resources use the same mechanism:
loader.getResource("config.properties")andgetResourceAsStreamsearch the loader's path. - Since Java 9 the extension loader is gone and the platform loader replaced it. In Java 8 the middle loader was called the "extension" loader.