Diff between ClassNotFoundException & NoClassDefFoundError

Hi Friends,Can any of you please clarify my doubt around how different is a ClassNotFoundException from a NoClassDefFoundError.It would be great if I could have your thoughts and understanding around this.
[226 byte] By [sanaa] at [2007-10-2 22:06:20]
# 1
I think it's like this:ClassNotFoundException is thrown if you directly use a class which can't be found.NoClassDefFoundError is thrown if you directly use a class which directly or indirectly uses another class which can't be found.
ejpa at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 2
The API for the two classes gives information about when each is thrown. [java.lang]
ChuckBinga at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Yes but it's not at all clear what the difference is, is it?

Actually on further investigation it turns out that NoClassDefFoundError is thrown if the apparent name of the class according to its filename isn't the same as the binary name compiled into it.

See ClassLoader.defineClass().

ejpa at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 4
* NoClassDefFoundError? represents an unfound import* ClassNotFoundException? represents dynamic classloading http://servlets.com/archive/servlet/ReadMsg?msgId=53577&listName=tomcat-user
samue-1a at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> ClassNotFoundException is thrown if you directly use

> a class which can't be found.

I think this must create compile time error wihich is "can not find the symbol", is not it ?

> NoClassDefFoundError is thrown if you directly use a

> class which directly or indirectly uses another class

> which can't be found.

This also can create compile time error

samue-1a at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 6
No. We are discussing exceptions and errors which arise at run-time out of code which obviously must have compiled correctly at some point. Otherwise we couldn't be running it.
ejpa at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 7
> Yes but it's not at all clear what the difference is,> is it?It was to me.
ChuckBinga at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 8
and did that agree with reply #3?
ejpa at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 9

> No. We are discussing exceptions and errors which

> arise at run-time out of code which obviously must

> have compiled correctly at some point. Otherwise we

> couldn't be running it.

I know what you are discussing about.

I mean that : For instance you are using the instance of Frame class without importing java.awt package. So according to your claim(opinion), this create a run time error called ClassNotFoundException or NoClassDefFoundError. However, I claim that this can create compile time error absolutely called "can not find symbol". So I mean that be careful while doing generilizations.

Regards,

Mert

samue-1a at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 10

ClassNotFoundException: is thrown when classloader tries to load a class with class name as string and could not be found. Its dyanamic loading of a class.

NoClassDefFoundError: is thrown if the Java Virtual Machine or a ClassLoader instance tries to load the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

madhav_patila at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 11

The important difference it that one's a checked Exception, the other is a run time error.

ClassNotFoundException is thrown in circumstances where it's likely to be due to a configuration file error or even someone typing in the wrong name.

NoClassDefFound is thrown only if your program is flawed or wrongly installed.

ClassNotFoundException is likely to be something your program can recover from. The other is not.

malcolmmca at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...
# 12

> I know what you are discussing about.

> I mean that : For instance you are using the instance

> of Frame class without importing java.awt package. So

> according to your claim(opinion), this create a run

> time error called ClassNotFoundException or

> NoClassDefFoundError.

That wasn't not my 'claim (opinion)' at all, of course, as the quoted text makes perfectly clear. It was just a straw man and it was irrelevant.

> So I mean that be careful while doing generilizations.

So, I mean, be careful when you attribute claims to other people that they haven't made.

And let's try to stick to the point, shall we?

ejpa at 2007-7-14 1:23:01 > top of Java-index,Java Essentials,Java Programming...