Classnotfoun Vs No Class Found Error
Classnotfoun Vs No Class Found Error
ClassNotFoundException NoClassDefFoundError
1 ClassNotFoundException is Checked
(compile time) Exception in java. NoClassDefFoundError is a Error in java.
Error and its subclasses are regarded as
unchecked exceptions in java.
-java.lang.Object -java.lang.Object
-java.lang.Throwable -java.lang.Throwable
-java.lang.Exception -java.lang.Error
-java.lang.ReflectiveOperationException -java.lang.LinkageError
-java.lang.ClassNotFoundException -java.lang.NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError
ClassNotFoundException is NoClassDefFoundError is thrown
thrown when JVM tries to class when JVM tries to load class but no
from classpath but it unable to find definition of class could be found.
that class.
Or, you can simply say class was Or, you can simply say class
unavailable in classpath at was unavailable in classpath at
runtime. runtime.
Example/Programs/Scenarios where ClassNotFoundException
may be thrown in java>
1. Loadingclasses 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;
/*OUTPUT
*/
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) {
/*OUTPUT
*/
/*OUTPUT
*/
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
*/
class A {
}
C:\Users\ankitmittal01>e:
E:\>cd E:\workspace
E:\workspace>javac NoClassDefFoundErrorExample.java
E:\workspace>
Let’s say you have deleted A.class. Now, let's execute the program again >
E:\workspace>java NoClassDefFoundErrorExample
Exception in thread "main" java.lang.NoClassDefFoundError: : A
at NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:3)
Caused by: java.lang.ClassNotFoundException: A
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)
... 1 more
E:\workspace>
As we see NoClassDefFoundError was thrown when JVM tries to load A.class which >
was NOT available at runtime but
was available at compile time.
We will create another simple program to understand how RuntimeException occured in static block of
class can throw NoClassDefFoundError.
System.out.println("\n--------------\n");
try {
new A(); // It will throw java.lang.NoClassDefFoundError
} catch (Error error) {
error.printStackTrace();
}
class A {
static {
System.out.println("In static block of class A");
System.out.println(1 / 0); // It will throw
java.lang.ArithmeticException
}
/*
* Constructor of class A will never get executed
*/
A() {
System.out.println("In constructor of class A");
}
}
--------------
In the above program, first new A() at line 6 throwed ExceptionInInitializerError because
ArithmeticException occurred in initializing the class (i.e. in static block of class) then,
new A() at line 14 throwed NoClassDefFoundError because class initialization has already thrown error.
You must read ArithmeticException is an unchecked exception and it is propagated automatically in java.
3. We will create another program which uses commons-lang.jar and will throw NoClassDefFoundError .
NoClassDefFoundError is thrown when JVM tries to load class but no definition of class could be found.
}
}
E:\>cd E:\workspace
E:\workspace>javac NoClassDefFoundErrorExample.java
E:\workspace>
As soon as javac NoClassDefFoundErrorExample.java is called .class file is formed.
Let’s say you have unset the classpath. Now, let's execute the program again >
E:\workspace>set CLASSPATH=
E:\workspace>java NoClassDefFoundErrorExample
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/lang/StringUtils
at NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:5)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
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)
... 1 more
E:\workspace>