Java Reference ยท java.lang
The ClassLoader class
Reference for java.lang.ClassLoader: the delegation model, bootstrap, platform and application loaders, loading resources, custom loaders and common errors.
A ClassLoader finds the bytes of a class (usually from a .class file or a JAR), turns them into a Class object, and remembers the result. You rarely call one directly, but every new Foo() for a class not yet used triggers a class loader behind the scenes, and every ClassNotFoundException or NoClassDefFoundError is a class loader failing to find something.
Java uses parent delegation: a loader first asks its parent to load a class and only searches its own sources if the parent cannot. This guarantees that java.lang.String always comes from the JDK and cannot be replaced by an application's own version.
ClassLoader loader = Main.class.getClassLoader(); Class<?> c = loader.loadClass("java.util.ArrayList");
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.
Syntax
public abstract class ClassLoader
| Member | Description |
|---|---|
loadClass(String name) |
Loads a class by binary name, asking the parent first. Throws ClassNotFoundException. |
findClass(String name) |
The method you override in a custom loader to find and define a class. |
defineClass(String, byte[], int, int) |
Turns bytes into a Class (protected). |
getParent(), getName() |
The parent loader and the loader's name (since Java 9). |
getResource(String), getResourceAsStream(String) |
Finds files on the class path, such as configuration. Returns null if absent. |
getResources(String) |
All matching resources (an enumeration of URLs). |
static getSystemClassLoader() |
The application class loader. |
static getPlatformClassLoader() |
The platform loader (since Java 9). |
getUnnamedModule(), getDefinedPackage(String) |
Module and package details. |
Usage notes
- Three built-in loaders (Java 9+): the bootstrap loader (core
java.baseclasses, represented bynullingetClassLoader()), the platform loader (other JDK modules) and the application loader (your class path). - A class's identity is its name plus its loader. Two loaders can each load
com.app.Plugin, giving two unrelated types; casting between them throwsClassCastExceptionwith a confusing message. Application servers and plugin systems use this for isolation. - Errors worth knowing.
ClassNotFoundException(checked) comes from explicit loading by name;NoClassDefFoundError(anError) means a class that was present when compiling is missing at run time;LinkageErrorandExceptionInInitializerErrorsignal problems while linking or running static initializers. - Resources.
Main.class.getResourceAsStream("/config.properties")uses an absolute path;ClassLoader.getResourceAsStream("config.properties")takes a path with no leading slash. - Context class loader.
Thread.getContextClassLoader()lets frameworks load classes of the application from library code. Use it for service-style lookups such asServiceLoader. - Writing a custom loader. Override
findClass, notloadClass, so delegation keeps working. Name your loader for readable diagnostics. - Memory leaks. A class and its loader can only be garbage collected together; static references from long-lived objects keep a whole loader (and every class it loaded) alive, a typical leak in hot-redeploy servers.
URLClassLoaderloads from JAR files and directories and is closeable; the application loader itself is no longer aURLClassLoadersince Java 9.
Related
Class,Thread,Runtime- Lessons: Reflection, Properties and configuration