This post will help us to get knowledge/distinguish between two similar but different types of problems that come up in our java code.
These both problems will happen at run time if required class is missed out.
From name it self we can say one is of type exception and one is of type error.
ClassNotFoundException: It will occur when required class is not available in class path when we try to load the class using class.forname() or loadClass() methods.
This Exception will thrown by application if it tries to load specific class during rutime using Class.forName() or loadClass() or findSystemClass() and the specified class is not available in class path.
For Example: We might come across this exception when we try to connect to databases like Mysql or Oracle etc. In this we load jdbc driver class of particular database by using Class.forName("com.mysql.jdbc.driver"); and we will add mysql.jar file in our class path. Here if we miss to add required jar file in our class path, our code will give this exception.
package com.sample.javase.testing;
public class ClassNotFoundException {
public static void main(String[] args) throws java.lang.ClassNotFoundException {
Class.forName("com.mysql.jdbc.driver");
}
}
Here if we did not add any jar file to class path, we will got below exception if we run this program.
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.sample.javase.testing.ClassNotFoundException.main(ClassNotFoundException.java:6)
NoClassDefError: It will occur when required class is available during compile time and if it is not available during run time.
It is thrown when JRE tries to load definition of class and the definition of class is not available during run time. The required class definition is available during the compile time but not available during runtime.
If we compile below program and generate .class files.
package com.sample.javase.testing;
public class NoClassDefErrorTest {
}
Here we already have NoClassDefErrorTest.class file. Hence below program also will compile successfully.
package com.sample.javase.testing;
public class NoClassDefErrorDemo {
public static void main(String[] args) {
NoClassDefErrorTest classDefErrorTest = new NoClassDefErrorTest();
}
}
And if we remove the NoClassDefErrorTest.class file from folder and if we run NoClassDefErrorDemo, then we will get below excpetion. Which means during
Exception in thread "main" java.lang.NoClassDefFoundError: com/sample/javase/testing/NoClassDefErrorTest
at com.sample.javase.testing.NoClassDefErrorDemo.main(NoClassDefErrorDemo.java:7)
Caused by: java.lang.ClassNotFoundException: com.sample.javase.testing.NoClassDefErrorTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
These both problems will happen at run time if required class is missed out.
From name it self we can say one is of type exception and one is of type error.
ClassNotFoundException: It will occur when required class is not available in class path when we try to load the class using class.forname() or loadClass() methods.
This Exception will thrown by application if it tries to load specific class during rutime using Class.forName() or loadClass() or findSystemClass() and the specified class is not available in class path.
For Example: We might come across this exception when we try to connect to databases like Mysql or Oracle etc. In this we load jdbc driver class of particular database by using Class.forName("com.mysql.jdbc.driver"); and we will add mysql.jar file in our class path. Here if we miss to add required jar file in our class path, our code will give this exception.
package com.sample.javase.testing;
public class ClassNotFoundException {
public static void main(String[] args) throws java.lang.ClassNotFoundException {
Class.forName("com.mysql.jdbc.driver");
}
}
Here if we did not add any jar file to class path, we will got below exception if we run this program.
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.sample.javase.testing.ClassNotFoundException.main(ClassNotFoundException.java:6)
NoClassDefError: It will occur when required class is available during compile time and if it is not available during run time.
It is thrown when JRE tries to load definition of class and the definition of class is not available during run time. The required class definition is available during the compile time but not available during runtime.
If we compile below program and generate .class files.
package com.sample.javase.testing;
public class NoClassDefErrorTest {
}
Here we already have NoClassDefErrorTest.class file. Hence below program also will compile successfully.
package com.sample.javase.testing;
public class NoClassDefErrorDemo {
public static void main(String[] args) {
NoClassDefErrorTest classDefErrorTest = new NoClassDefErrorTest();
}
}
And if we remove the NoClassDefErrorTest.class file from folder and if we run NoClassDefErrorDemo, then we will get below excpetion. Which means during
Exception in thread "main" java.lang.NoClassDefFoundError: com/sample/javase/testing/NoClassDefErrorTest
at com.sample.javase.testing.NoClassDefErrorDemo.main(NoClassDefErrorDemo.java:7)
Caused by: java.lang.ClassNotFoundException: com.sample.javase.testing.NoClassDefErrorTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
No comments:
Post a Comment