
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - ClassNotFoundException
Imagine youâre watching a movie, and suddenly a character is mentioned who hasnât appeared yet. You pause and check the cast list, but⦠they donât exist! Java feels the same way when itâs asked to find a class that isnât thereâit throws a ClassNotFoundException.
In technical terms, this happens when Java tries to load a class dynamically (meaning at runtime, not during compilation), but it canât find it in the classpath.
Constructors of ClassNotFoundException
There are two constructors of the ClassNotFoundException class:
- ClassNotFoundException(): This constructor is used to create a ClassNotFoundException object without any message.
- ClassNotFoundException(String message): This constructor is used to create a ClassNotFoundException object with a message.
How do you get a ClassNotFoundException?
There are several ways to get a ClassNotFoundException:
- You use Class.forName("SomeClass"), but SomeClass isnât available.
- You call ClassLoader.loadClass("SomeClass"), and Java canât locate it.
- A required JAR file is missing from your project.
Example of ClassNotFoundException using Class.forName()
In this example, we are trying to load a class that doesnât exist, so JVM will throw a ClassNotFoundException.
public class ClassNotFoundExample { public static void main(String[] args) { Class.forName("com.tutorialspoint.Test); } }
Following is the output of the above code:
ClassNotFoundExample.java:3: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown Class.forName("com.tutorialspoint.Test"); ^ 1 error
As you can see in the output, JVM throws a ClassNotFoundException because we are trying to load a class that doesnât exist.
Example of ClassNotFoundException using ClassLoader.loadClass()
In this example, we are trying to load a class that doesnât exist, so JVM will throw a ClassNotFoundException.
public class ClassNotFoundExample { public static void main(String[] args) { ClassLoader.getSystemClassLoader().loadClass("com.tutorialspoint.Test"); } }
Following is the output of the above code:
ClassNotFoundExample.java:3: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown ClassLoader.getSystemClassLoader().loadClass("com.tutorialspoint.Test"); ^ 1 error
How to handle ClassNotFoundException
In this example, we are trying to load a class that doesnât exist, so JVM will throw a ClassNotFoundException. We are handling this exception using a try-catch block.
public class ClassNotFoundExample { public static void main(String[] args) { try { Class.forName("com.tutorialspoint.Test"); } catch(ClassNotFoundException e) { System.out.println("Class not found"); } } }
Following is the output of the above code:
Class not found
How to avoid ClassNotFoundException?
Here are some ways to avoid ClassNotFoundException:
- Check the classpath to ensure the required class is available.
- Check the spelling of the class name.
- Check the package name of the class.
By following the above ways, you can avoid the ClassNotFoundException in Java.