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:

  1. ClassNotFoundException(): This constructor is used to create a ClassNotFoundException object without any message.
  2. 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.

java_lang_exceptions.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy