Friday, June 19, 2015

ClassNotFoundException

In this tutorial we will discuss about ClassNotFoundException. This exception is thrown when an application tries to load a class through its string name, but no definition for the specified class name could be found. A class can be loaded using one of the following methods:



This exceptions extends the ReflectiveOperationException, which is defined as the common superclass of exceptions thrown by reflective operations in core reflection. Finally, after the Java 1.4 release, the ClassNotFoundException has been retrofitted to conform to the general purpose exception-chaining mechanism. The raising exception may be accessed via the Throwable.getCause() method.

The ClassNotFoundException in Java

The ClassNotFoundException is thrown when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. The ClassNotFoundException is a checked exception and thus, must be declared in a method or constructor’s throws clause.
The following example tries to load a class using the forName method. However, the specified class name cannot be found and thus, a ClassNotFoundException is thrown.
ClassNotFoundExceptionExample.java:
01package main.java;
02
03public class ClassNotFoundExceptionExample {
04
05    private static final String CLASS_TO_LOAD = "main.java.Utils";
06
07    public static void main(String[] args) {
08        try {
09            Class loadedClass = Class.forName(CLASS_TO_LOAD);
10            System.out.println("Class " + loadedClass + " found successfully!");
11        }
12        catch (ClassNotFoundException ex) {
13            System.err.println("A ClassNotFoundException was caught: " + ex.getMessage());
14            ex.printStackTrace();
15        }
16    }
17}
A sample execution is shown below:
A ClassNotFoundException was caught: main.java.Utils
java.lang.ClassNotFoundException: main.java.Utils
 at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:259)
 at main.java.ClassNotFoundExceptionExample.main(ClassNotFoundExceptionExample.java:9)

How to deal with the ClassNotFoundException

  • Verify that the name of the requested class is correct and that the appropriate .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.
  • In case the specified .jar file exists in your classpath then, your application’s classpath is getting overriden and you must find the exact classpath used by your application.
  • In case the exception is caused by a third party class, you must identify the class that throws the exception and then, add the missing .jar files in your classpath.

No comments:

Post a Comment