NoClassDefFoundError is a error ClassNotFoundException is a exception
no class def found..this would be encountered when you don't have the class in ur classpath...can be resolved when you change ur classpath to have the appropriate class in classpath. ClassNotFoundException..This would be encountered when you have same classes loaded by two diff classloader and the JVM is confused which one to load.. This is different from ClassCastException.
--this is my assumption..let me know what you feel.
Consider, you have two custom classloaders - CL1 and CL2. A class loaded by CL1 when tried to be accessed by CL2 will also throw a ClassNotFoundException.
And consider you have Class1 loaded by both CL1 and CL2, they both are not compatible even through they are the same. There we get ClassCastException.
ClassNotFoundException is genarlly thrown whenyou try load the class via its String name like Class.forName ,loadClass ,findSystemClass etc... else it is the NoClassDefFoundError
It is the case of static vs dynamic classsloading. If it is static classloading,then NoClassDefError is expected otherwise, ClassNotFoundException is expected. You can always override this rule by putting your custom class loader and throwing NoClassDefError instead of ClasNotFoundException. But it is advisable to throw Exception (for custom classloaders) since ClassNotFoundException is checked exception and the caller must be forced to take appropriate action.
When you load class by Class.forName(..) or by calling loadClass() thru your own class loader, it is called dynamic class loading, otherwise, it is static.
6 comments:
NoClassDefFoundError is a error ClassNotFoundException is a exception
no class def found..this would be encountered when you don't have the class in ur classpath...can be resolved when you change ur classpath to have the appropriate class in classpath.
ClassNotFoundException..This would be encountered when you have same classes loaded by two diff classloader and the JVM is confused which one to load..
This is different from ClassCastException.
--this is my assumption..let me know what you feel.
Further
Consider, you have two custom classloaders - CL1 and CL2. A class loaded by CL1 when tried to be accessed by CL2 will also throw a ClassNotFoundException.
And consider you have Class1 loaded by both CL1 and CL2, they both are not compatible even through they are the same. There we get ClassCastException.
ClassNotFoundException is genarlly thrown whenyou try load the class via its String name like Class.forName ,loadClass ,findSystemClass etc...
else it is the NoClassDefFoundError
ClassNotFoundException is a checked exception of loadclass method.so as you have said in your comments its look fine
I am not still clear on NoClassDefFoundError
for example,
java ttt(any class name thats not present)
Exception in thread "main" java.lang.NoClassDefFoundError: ttt
Also,is it possible that a same class is loaded by two different classloaders???
please commentt ......
To make it simple:
It is the case of static vs dynamic classsloading. If it is static classloading,then NoClassDefError is expected otherwise, ClassNotFoundException is expected. You can always override this rule by putting your custom class loader and throwing NoClassDefError instead of ClasNotFoundException. But it is advisable to throw Exception (for custom classloaders) since ClassNotFoundException is checked exception and the caller must be forced to take appropriate action.
Comment.
tapan,
please tell this
static classloading.. ?
i dont have any clue regarding this
When you load class by Class.forName(..) or by calling loadClass() thru your own class loader, it is called dynamic class loading, otherwise, it is static.
Post a Comment