What is ClassDefNotFoundException and NoClassDefFoundError and explain differences between these two?

Questions by sweet_person

Showing Answers 1 - 6 of 6 Answers

g4java

  • Sep 14th, 2007
 

Given a class A..

A ClassNotFoundException means that the classloader cannot load class A.

A ClassDefNotFoundError means that the classloader can load class A, but cannot instantiate it because it cannot load the other classes that class A depend on.

tpran1685

  • May 26th, 2008
 

a. A NoClassDefFoundException isthrown if a class is referenced with Java’s “new” operator (i.e. static loading) but the runtime system cannot find the referenced class.

b. A ClassNotFoundException is thrown when an application tries to load in a class through its string name using the following methods but no definition for the class with the specified name could be found:
i)The forName(..) method in class - Class.
ii)The findSystemClass(..) method in class - ClassLoader.
iii)The loadClass(..) method in class - ClassLoader.

aveekgeek

  • Jan 29th, 2009
 

Both mean that the ClassLoader is able to load the Class but not able to
instantiate it because of unavialability of some dependent class.


ClassDefNotFoundException is something that can be recovered from whereas
NoClassDefFoundError can't be recovered from.


Thanks,
Aveek

  Was this answer useful?  Yes

pradeep013

  • Feb 21st, 2011
 

If these error are found means your class file is not available for JVM so confirm location of your .class file, then try to run program.
If this error is found again then use "set classpath=.;"

  Was this answer useful?  Yes

PriyankN

  • Mar 10th, 2011
 

To understand this better, run this example by keeping only one block uncommented,compiling and then removing class Test2 before running:


public class NoClassDefFoundTest
{
public static void main(String args[]) throws Exception
{
/* //Block1:Throws NoClassDefFoundError if Test2 is removed after compilation
Test2 b = new Test2();
//int a = b.i;
*/
// Block2:Throws ClassNotFoundException if Test2 is removed after compilation
Class c = Class.forName("Test2");
c.getName();
}
}

class Test2{}

Ganga_busi

  • Oct 25th, 2012
 

A NoClassDefFoundException is thrown if a class is referenced with Java’s "new" operator (i.e. static loading) but the runtime system cannot find the referenced class.(that means whenever the JVM trying to access the .class file it is not available in that location)

A ClassNotFoundException is thrown when an application tries to load in a class through its string name using the following methods but no definition for the class with the specified name could be found:

The forName(..) method in class - Class.(ex: class.forName("sample");(Here sample is a class name which is created by user.))

The findSystemClass(..) method in class - ClassLoader.

The loadClass(..) method in class - ClassLoader.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions