您的位置:首页 > Web前端

Difference between ClassNotFoundException and NoClassDefFoundError

2012-05-27 17:25 477 查看
dojo.addOnLoad(function(){
dojo.query('.entryContentContainer img').forEach(
function(item){
if(item.style != undefined && item.style.width != undefined &&
item.style.width == "100%")
return;
if (item.width > 400 && item.complete) {
item.height = item.height * (400/item.width);
item.width = 400;
}
if(!item.complete){
item.onload = function(){
if (this.width > 400) {
this.height = this.height * (400/this.width);
this.width = 400;
}
};
}
}
);
});
Difference between ClassNotFoundException and NoClassDefFoundErrorPriyanka_JTC | document.write(blogsDate.date.localize (1321596800000));Nov 18 2011 | Comments (0) | Visits (333)Difference between ClassNotFoundException and NoClassDefFoundErrorClassNotFoundException is a checked Exception, Its thrown when that required class is not present in the location where classloader is looking.
When an application tries to load in a class through its string name using:
-The forName() method in class Class.
-The findSystemClass method() in class ClassLoader.
-The loadClass() method in class ClassLoader.
but no definition for the class with the specified name could be found.This exception can be created using following program-
import java.net.URL;
import java.net.URLClassLoader;
public class ClassNotFoundExceptionTest {
public static void main(String args[]) {
try {
URLClassLoader loader = new URLClassLoader(new URL[] { new URL(
"file://C:/CL/ClassNotFoundException/")});
loader.loadClass("DoesNotExist");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
 To avoid this error , User should check the following -
- Make sure that
Class is available in the logical class path of the class loader associated with the class.
- Make sure that
Class Loader API is used properly .ie whether a wrong class Loader is engaged in Class.forname().
- Make sure that dependent class of the class being loaded is visible to the class loader.NoClassDefFoundError is an error .Its thrown to indicate that ClassLoader instance is trying to load in the definition of a class and no definition of the class is found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.This error can be recreated using following test program –public class NoClassDefFoundErrorTest {
    public static void main(String[] args) {
        A a = new A();
  }
}
public class A extends B { }
public class B { }
//Once you've compiled the above code remove the classfile for B. When //the code is executed, the following error occurs
//Exception in thread "main" java.lang.NoClassDefFoundError: B
Possible reasons of the error-
- Bad format of the class
- The version number of a class not matching- This can happen in the distribution or production of JAR files, where not allthe required class files were included.User should check whether a Class is not available on the classpath(not missing in the jar file). This problem can occur also when Class cannot load .
There are various reasons the class could not be loaded. Possible reasons include the following:
- Failure to load the dependent class
- The dependent class has a bad format.
- The version number of the class is incorrect.The difference between the two is that one is an
Error
and the other is an
Exception
.
NoClassDefFoundError
is an
Error
and it arises from fact the Java Virtual Machine having problems finding a class it expected to find. A program that was expected to work at compile-time can't run because of
class
files not being found, or is not the same as was produced or encountered at compile-time. This is a critical error, as the program cannot be initiated by the JVM.On the other hand, the
ClassNotFoundException
is an
Exception
, which is expected, and recoverable.
https://www.ibm.com/developerworks/mydeveloperworks/blogs/738b7897-cd38-4f24-9f05-48dd69116837/entry/difference_between_difference_between_classnotfoundexception_and_noclassdeffounderror29?lang=enhttp://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐