when java.lang.ClassNotFoundException occurs in java

What is hierarchy of java.lang. ClassNotFoundException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.ReflectiveOperationException
   -java.lang.ClassNotFoundException



ClassNotFoundException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.ClassNotFoundException is a Checked (compile time) Exception in java.


What is ClassNotFoundException in java?

ClassNotFoundException is thrown when JVM tries to class from classpath but it unable to find that class.
Or, you can simply say class was unavailable in classpath at runtime..


Which methods can throw ClassNotFoundException in java?

ClassNotFoundException is thrown whenever an java application tries to load a class by passing class name as String in following methods -
  1. forName(String className) method of java.lang.Class class.
  2. findSystemClass(String name) method of java.lang.ClassLoader class.
  3. loadClass(String name) method of java.lang.ClassLoader class.


Example/Programs/Scenarios where ClassNotFoundException may be thrown in java>

  1. Loading classes via Reflection in java > ClassNotFoundException is thrown whenever an application tries to load a class by passing class name as String in forName(String className) method of Class class.
Example -
package ClassNotFoundExceptionExamplePkg;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ClassNotFoundExceptionExample {
public static void main(String... a) {
     System.out.println("initialize 'ReflectionClass' at runtime");
     // initialize 'ReflectionClass' at runtime
     Class cls = null;
     try {
          cls = Class.forName("ClassNotFoundExceptionExamplePkg.ReflectionClass");
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     }
}
}
/*OUTPUT
initialize 'ReflectionClass' at runtime
java.lang.ClassNotFoundException: ReflectionClass
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at ClassNotFoundExceptionExamplePkg.ClassNotFoundExceptionExample.main(ClassNotFoundExceptionExample.java:12)
*/


Solution - Make class available in package ClassNotFoundExceptionExamplePkg and call forName(String className) method of java.lang.Class class.

Example -
package ClassNotFoundExceptionExamplePkg;
class ReflectionClass {
static{
     System.out.println("ReflectionClass has been initialized");
}
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class ClassNotFoundExceptionAvoidExample {
public static void main(String... a) {
     System.out.println("initialize 'ReflectionClass' at runtime");
     // initialize 'ReflectionClass' at runtime
     Class cls = null;
     try {
          cls = Class.forName("ClassNotFoundExceptionExamplePkg.ReflectionClass");
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     }
}
}
/*OUTPUT
initialize 'ReflectionClass' at runtime
ReflectionClass has been initialized
*/


  1. ClassNotFoundException can be thrown when java application tries to load/initialize oracle.jdbc.driver.OracleDriver class by passing class name as String in Class.forName("oracle.jdbc.driver.OracleDriver") method of Class class but OracleDriver class is not present in classpath.
Example -
package ClassNotFoundExceptionExamplePkg2;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyJdbcConnectionSetup {
public static void main(String... arg) {
     System.out.println("register Oracle driver class, i.e. initialize OracleDriver");
     try {
          // register Oracle driver class, i.e. initialize OracleDriver
          Class.forName("oracle.jdbc.driver.OracleDriver");
          System.out.println("Oracle driver class registered, i.e. initialized OracleDriver");
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     }
}
}
/*OUTPUT
register Oracle driver class, i.e. initialize OracleDriver
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at ClassNotFoundExceptionExamplePkg2.MyJdbcConnectionSetup.main(MyJdbcConnectionSetup.java:8)
*/


Solution - Make OracleDriver class available in classpath. Read how to  configure java build path - registering driver



Example -
package ClassNotFoundExceptionExamplePkg2;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyJdbcConnectionSetup {
public static void main(String... arg) {
     System.out.println("register Oracle driver class, i.e. initialize OracleDriver");
     try {
          // register Oracle driver class, i.e. initialize OracleDriver
          Class.forName("oracle.jdbc.driver.OracleDriver");
          System.out.println("Oracle driver class registered, i.e. initialized OracleDriver");
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     }
}
}
/*OUTPUT
register Oracle driver class, i.e. initialize OracleDriver
Oracle driver class registered, i.e. initialized OracleDriver
*/


How to avoid ClassNotFoundException in java?


  1. Ensure classpath environment variable is not overridden or changed.
  2. Make sure class to be loaded via reflection is in correct package and spelled correctly.

RELATED LINKS>


Writing and executing first java program through CMD in windows, Setting up ECLIPSE in java, Directory of .class file

Reflection in java - Methods used and program


No comments:

Post a Comment