Javatpoint Logo
Javatpoint Logo

ClassNotFoundException Java

Java is a popular programming language used by developers around the world to build a wide range of applications. Despite its popularity and reliability, Java programs are prone to errors and exceptions. One of the most common exceptions in Java is the ClassNotFoundException. In this article, we will explore the ClassNotFoundException in Java, understand its causes and effects, and provide some tips to handle this exception.

What is ClassNotFoundException in Java?

ClassNotFoundException is a runtime exception that occurs when the Java Virtual Machine (JVM) cannot find the class specified in the code. This exception typically occurs when the Java classloader fails to load a class at runtime.

When a Java program is compiled, the Java compiler checks if the referenced classes exist and whether they are valid. However, the Java classloader is responsible for loading the classes into the JVM at runtime. If the classloader cannot find the specified class, it throws a ClassNotFoundException.

Causes of ClassNotFoundException

There are several reasons why the Java classloader may fail to load a class, resulting in a ClassNotFoundException. Some of the common causes of this exception include:

  1. Incorrect class name or package name: One of the most common causes of ClassNotFoundException is an incorrect class or package name specified in the code. If the class name or package name is misspelled or does not exist, the classloader cannot find the specified class.
  2. Missing class file: Another reason why the classloader may fail to load a class is if the corresponding class file is missing or corrupted. If the class file is not present in the classpath, the classloader cannot load the class.
  3. Incorrect classpath: The classpath is a set of directories and JAR files that the classloader searches for classes at runtime. If the classpath is incorrect or does not contain the required classes, the classloader cannot load the classes.
  4. Version conflicts: If there are multiple versions of the same class in the classpath, the classloader may fail to load the correct version of the class. This can result in a ClassNotFoundException.

Let's now take a look at some example programs to demonstrate the ClassNotFoundException in Java.

Example 1:

In this example, we will try to load a non-existent class "NonExistentClass" using the Class.forName() method. This will result in a ClassNotFoundException.

Filename: ClassNotFoundExceptionExample.java

Output:

java.lang.ClassNotFoundException: NonExistentClass
	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.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:398)
	at ClassNotFoundExceptionExample.main(ClassNotFoundExceptionExample.java:4) 

As you can see from the output, the classloader throws a ClassNotFoundException when it cannot find the specified class.

Example 2:

In this example, we will try to load a class "com.example.MyClass" that is not present in the classpath.

Filename: ClassNotFoundExceptionExample.java

Output:

java.lang.ClassNotFoundException: com.example.MyClass
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at Main.main(Main.java:5) 

As you can see from the output, the classloader cannot find the specified class "com.example.MyClass" in the classpath and throws a ClassNotFoundException.

Handling ClassNotFoundException

Now we understand the causes and effects of the ClassNotFoundException in Java, let's take a look at some tips to handle this exception.

  1. Check the class name and package name: One of the most common causes of ClassNotFoundException is an incorrect class or package name specified in the code. Double-check the class and package names to ensure they are correct.
  2. Check the classpath: The classpath is a set of directories and JAR files that the classloader searches for classes at runtime. Make sure the classpath is correctly set and contains the required classes.
  3. Check the class file: If the class file is missing or corrupted, the classloader cannot load the class. Make sure the class file is present in the classpath and is not corrupted.
  4. Use the correct version of the class: If there are multiple versions of the same class in the classpath, make sure you are using the correct version of the class.
  5. Use the fully-qualified class name: When using the Class.forName() method to load a class, make sure you use the fully-qualified class name, including the package name.
  6. Use try-catch blocks: When using the Class.forName() method or any other method that may throw a ClassNotFoundException, use try-catch blocks to handle the exception gracefully.

ClassNotFoundException is a common exception in Java that occurs when the classloader cannot find the specified class at runtime. This exception can be caused by a variety of factors, including incorrect class or package names, missing class files, incorrect classpaths, and version conflicts. To handle this exception, you should check the class name and package name, check the classpath, use the correct version of the class, use the fully-qualified class name, and use try-catch blocks to handle the exception gracefully.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA