The 30 second answer
Class loaders find class bytes and define classes. Java has three built-in ones: bootstrap for the core platform, platform for the rest of the Java SE modules, and the application loader for the class path. Each loader asks its parent first, so core classes cannot be replaced. ClassNotFoundException means an explicit lookup failed; NoClassDefFoundError means a class that existed at compile time is missing at run time.
Loading, linking, initialising. The JVM specification splits a class's start into loading (find the bytes and create the class), linking (verify, prepare static fields, resolve references) and initialising (run static initialisers). Loading is done by a class loader, and a class's run-time identity is its name plus its defining loader, so the same class name loaded by two loaders gives two different types.
The built-in loaders. The ClassLoader Javadoc lists three:
- Bootstrap: the virtual machine's built-in loader, "typically represented as
null", with no parent. It loads core classes such asjava.lang.String. - Platform: loads the remaining Java SE platform APIs and JDK classes.
- System (application): loads classes from the application class path and module path.
Parent delegation. The Javadoc: a loader "will usually delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself." So an application cannot replace java.lang.String with its own copy. The default loadClass algorithm does this in order: it checks whether the class is already loaded, then invokes loadClass on the parent (or the bootstrap loader if the parent is null), and only then calls findClass to load it itself. Parent-first delegation keeps core classes consistent and is the usual security boundary around platform types.
When the order is inverted. The Javadoc says "usually" because some loaders do not ask the parent first. Servlet containers often use child-first delegation for a web application's own classes, so the app can ship its own version of a library instead of seeing the container's copy first. That is a deliberate exception, not the platform default.
public class Loaders {
public static void main(String[] args) throws Exception {
System.out.println("String -> " + String.class.getClassLoader()); // bootstrap: null
System.out.println("java.sql -> " + java.sql.Connection.class.getClassLoader().getName());
ClassLoader app = Loaders.class.getClassLoader();
System.out.println("Loaders -> " + app.getName() + ", parent " + app.getParent().getName());
try {
Class.forName("com.example.Missing");
} catch (ClassNotFoundException e) {
System.out.println("forName -> " + e);
}
}
}Verified with OpenJDK 21.0.13. It prints String -> null, java.sql -> platform, Loaders -> app, parent platform, and java.lang.ClassNotFoundException: com.example.Missing.
Reading the two errors.
ClassNotFoundExceptionis a checked exception from an explicit lookup such asClass.forNameorloadClass: the name is wrong, or the JAR is not on the path. Common with JDBC drivers and plugins.NoClassDefFoundErroris an error: the class existed when the calling code was compiled, but its definition "can no longer be found" at run time. The JVM specification says that when loading fails because the class cannot be found, it throwsNoClassDefFoundErrorwith theClassNotFoundExceptionas its cause. The other common cause is a static initialiser that threw: the first use fails withExceptionInInitializerError, later uses withNoClassDefFoundError. Always read the cause.
Where it matters in production. Dependency conflicts (two versions of a library, the wrong one wins), application servers and plugin systems with a loader per application, and memory leaks when something holds a reference to an old loader so none of its classes can be unloaded after a redeploy. A Spring Boot fat JAR uses its own launcher class loader to load the nested JARs inside it; see fat JAR vs normal JAR.
Alternate interview scenarios
"What is the difference between Exception and Error? Then: NoClassDefFoundError versus ClassNotFoundException."
Throwable. The Error Javadoc says an error "indicates serious problems that a reasonable application should not try to catch", such as OutOfMemoryError or StackOverflowError. An Exception is a condition the application may handle. Exceptions that are not subclasses of RuntimeException are checked, so the compiler makes you declare or catch them. Runtime exceptions and errors are unchecked.That split explains the two class-loading failures. ClassNotFoundException is a checked exception. It's thrown when code asks for a class by name, with Class.forName or ClassLoader.loadClass, and no loader can find it. The caller can reasonably handle that, for example by skipping an optional plugin. NoClassDefFoundError is an error, a LinkageError. The class existed when the calling code was compiled, but the JVM can't find or define it now. Usually a JAR is missing at runtime, or the class's static initializer already failed: the first use throws ExceptionInInitializerError, and every later use throws NoClassDefFoundError. I don't catch it. I look for the first error in the log and fix the classpath or the initializer.
Sources (6)
- ClassLoader Javadoc (Java 21): delegation model and built-in class loadersdocs.oracle.com
- JVM Specification SE 21, chapter 5: Loading, Linking, and Initializingdocs.oracle.com
- NoClassDefFoundError Javadoc (Java 21)docs.oracle.com
- Spring Boot reference: The executable JAR formatdocs.spring.io
- Error Javadoc (Java 21)docs.oracle.com
- Exception Javadoc (Java 21): checked exceptionsdocs.oracle.com