Java ClassNotFoundException
Example
Load a class that does not exist, and catch the exception:
public class Main {
public static void main(String[] args) {
try {
Class.forName("NoSuchClass");
} catch (ClassNotFoundException e) {
System.out.println("The class was not found.");
}
}
}
Definition and Usage
A ClassNotFoundException happens when a program loads a class by its name (as text), for example with Class.forName(), and the class cannot be found.
It often happens when the class name is spelled wrong, or when a library (a .jar file with ready-made classes) is missing. It is a checked exception (Java requires you to catch it, or to declare it with throws).
Related Pages
Java Exceptions (try...catch) Tutorial
❮ Errors & Exceptions