ClassNotFoundException is one of the most commonly occurring exceptions. This exception is thrown when the executing program is unable to find the defined class. This could be because of various reasons, wrong classpath, older version of jar file etc...
packageorg.training.exceptions;
public class ClassNotFound {
public static void main(String[] args) {
new ClassNotFound();
}
publicClassNotFound(){
try {
// load the java.lang.Strung class,
// this would thrown an ClassNotFoundException
because there is no class called “Strung” defined in the java.lang package
Class c = Class.forName("java.lang.Strung");
}catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
Output:
java.lang.ClassNotFoundException: java.lang.Strung
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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 org.training.exceptions.ClassNotFound.<init>(ClassNotFound.java:14)
at org.training.exceptions.ClassNotFound.main(ClassNotFound.java:7)
No comments:
Post a Comment