CoursesJVM internals for Kotlin and Android developers
Puzzle 15 · 16 of 27
Not found, or no longer there?
Puzzles 11 and 14 showed that a class is loaded at its first use, by a loader searching the class path. Run the same small app two ways with a class missing, and learn what each error says about who asked and why nothing was found.
All puzzles in this course
The problem
Puzzle 14 followed a request for a class up the chain of loaders. Sometimes nobody has the class. An app ran fine on your machine. On a server it starts, serves requests for an hour, then crashes with NoClassDefFoundError. Another time it's ClassNotFoundException. Both sound like "class missing". Are they the same problem? And why didn't the JVM refuse to start if a class was missing all along?
See it
Scroll sideways to see the full diagram.
Run 1: Helper.class was deleted. main prints start, then reaches
invokestaticHelper.bonus. The JVM must resolve Helper, a class named in App's compiled bytecode.Step 1 of 5The JVM asks the application loader for Helper, and the loader searches the class path, the folder out. Nothing is there.
Step 2 of 5The loader throws
ClassNotFoundExceptionback to the JVM. Predict: what does App's code receive?Step 3 of 5The JVM wraps it:
NoClassDefFoundError, caused by theClassNotFoundException. Compiled code depended on Helper, so it's an Error.Step 4 of 5Run 2: App's own code passes the name "Missing" to Class.forName. The JVM resolves nothing; the loader's
ClassNotFoundExceptioncomes straight back to App.Step 5 of 5
The building block
Both names in this puzzle are things Java throws, from the two families of puzzle 13: ClassNotFoundException is an Exception, which the caller is expected to handle; NoClassDefFoundError is an Error, a broken deployment that ordinary code shouldn't catch. That difference is the whole story.
Real class paths list jars more often than folders: a jar is one zip file holding a library's class files, and the class path may list jar files and folders side by side. Leaving a jar out of a deployment removes every class in it at once.
Run 1: a class deleted from the class path. App calls Helper.bonus(). Both were compiled together; then Helper.class was deleted, the way a jar can be left out of a deployment. The two files:
public class Helper {
static int bonus() { return 3; }
}
public class App {
public static void main(String[] args) throws Exception {
System.out.println("start");
if (args.length > 0) {
Class<?> k = Class.forName(args[0]);
System.out.println("found " + k.getName());
return;
}
System.out.println(Helper.bonus());
}
}
$ java -cp out App
start
Exception in thread "main" java.lang.NoClassDefFoundError: Helper
at App.main(App.java:9)
Caused by: java.lang.ClassNotFoundException: Helper
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
... 1 more
start is printed: nothing needed Helper until line 9 (puzzle 11). There, the invokestatic for Helper.bonus must resolve the class, the application loader searches the class path, and finds nothing. The loader reports ClassNotFoundException; the JVM wraps it in NoClassDefFoundError, because compiled code depended on that class. Its documentation describes exactly this: the class "existed when the currently executing class was compiled, but the definition can no longer be found".
Run 2: a name in a string. Some code chooses classes at run time by name: plugin systems, serialization libraries, database drivers, dependency-injection libraries. They call Class.forName or a loader's loadClass with a string:
$ java -cp out App Missing
start
Exception in thread "main" java.lang.ClassNotFoundException: Missing
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
...
at java.base/java.lang.Class.forName(Class.java:414)
at App.main(App.java:5)
No NoClassDefFoundError this time. ClassNotFoundException is an Exception that methods like Class.forName declare, so the code that passed the name is expected to catch it and decide (try another driver, report a bad configuration). It says: "you asked for this name, and no loader had it".
ClassNotFoundException | NoClassDefFoundError | |
|---|---|---|
| Who asked | code, with a name in a string | the JVM, resolving a reference in compiled bytecode |
| What it means | that name was never found | the class was there at compile time and isn't usable now |
| Typical cause | wrong name, a driver or plugin not shipped | a jar left out at run time, or a failed static initializer (puzzle 13) |
| Who should handle it | the caller, which expected it might fail | usually nobody: fix the build or deployment |
A NoClassDefFoundError with "Could not initialize class" is the other case, from puzzle 13: the class was found, but its static initializer failed earlier. Read the message before reading the class path.
The edges
- Many apps use the class path. Anything started with -cp does, so the first-use behavior of run 1 is what you'll meet most often. Puzzle 16 shows the alternative that checks before main runs.
- The first matching class wins. The class path is searched in order. Two jars containing different versions of the same class don't cause an error: the first one listed is used, and a method that exists only in the other version fails later with a different error, NoSuchMethodError. In a run with two folders holding Helper, version 1 listed first, Helper.bonus() returned version 1's answer and the call to a method only version 2 has failed with NoSuchMethodError: 'int Helper.extra()'. When a library behaves like an older version than you expected, list the class path and look for duplicates.
- Fix the cause, not the catch. Catching
NoClassDefFoundErrorhides a broken deployment. CatchClassNotFoundExceptiononly where a name might legitimately be absent.
The analogy
Run 1 is a recipe book printed with "see page 40" on page 12, but page 40 was torn out. You only notice when you reach page 12, and the problem is the book, not you. Run 2 is a customer asking the clerk for a dish by name: the clerk says "we don't have that", and the customer is expected to pick something else.
Run it yourself
Save App and Helper above as App.java and Helper.java in one folder, then:
javac -d out App.java Helper.java
java -cp out App
rm out/Helper.class
java -cp out App
java -cp out App Missing
java -cp out App java.lang.String
Before each java command, predict whether start is printed, and which error (if any) follows it.
Check yourself
Sources
- Java 21 API: NoClassDefFoundError ("Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.")
- Java 21 API: ClassNotFoundException ("Thrown when an application tries to load in a class through its string name using: The forName method in class Class . The findSystemClass method in class ClassLoader . The loadClass method in class ClassLoader . but no definition for the class with the specified name could be found.")
- The jar command (JDK 21) ("The jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats.")
- The java command (JDK 21) ("-cp classpath Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
- Both runs, the java.lang.String run, and the two-version class path run were produced in this session with OpenJDK 21.0.12.
What you now know
On the class path, a missing class is discovered only when code first needs it. ClassNotFoundException means code asked a loader for a name in a string and nothing was found; NoClassDefFoundError means the JVM, resolving compiled code, needed a class that can no longer be found (it wraps the loader's ClassNotFoundException) or couldn't be initialized.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.