Hi,
I am a newbie to Java. I just installed Java on my laptop and ran some
example programs. And I got the message that says Exception in thread
"main" java.lang.NoClassDefFoundError even though this example program
is very simple and it's from a book. I would appreciate it if you give
me some hints why this happens.
Thank you very much.
Andrew Thompson - 30 Sep 2005 14:43 GMT
> I am a newbie to Java. I just installed Java on my laptop and ran some
> example programs. And I got the message that says Exception in thread
> "main" java.lang.NoClassDefFoundError
Roedy has handy collection of meanings of those compile[1] and
run-time[2] errors. This particular one is here..
<http://mindprod.com/jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR>
[1] <http://mindprod.com/jgloss/compileerrormessages.html#INDEX>
[2] <http://mindprod.com/jgloss/runerrormessages.html#INDEX>
>..even though this example program
> is very simple and it's from a book. I would appreciate it if you give
> me some hints why this happens.
Unfortunately you have struck out that has many causes.
Perhaps if you cannot figure what you are doing wrong
from the list of explanations, you might provide more
detail on..
- the exact paths that your .java and .class files are
- the 'package' statement in your code (if you put one in)
- the exact commands you are typing, AND..
- the path of the directory you are in when you are
issuing the commands.
Gordon Beaton - 30 Sep 2005 14:47 GMT
> I am a newbie to Java. I just installed Java on my laptop and ran some
> example programs. And I got the message that says Exception in thread
> "main" java.lang.NoClassDefFoundError even though this example program
> is very simple and it's from a book. I would appreciate it if you give
> me some hints why this happens.
Here's an educated guess: you typed "java MyProgram.class"
instead of "java MyProgram".
But really you need to provide more information. A good start is the
exact text of the error message, and the exact commands you used.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
thickface - 30 Sep 2005 15:57 GMT
First, thanks for the reply.
It is a really simple program, Hello World.
public class HelloWorldApp
{
public static void main (String arg[]){
System.out.println("Hello World");
}
}
Actually, i ran other programs too. I was able to compile all the
programs , and, in this particular case, the command i used is
java HelloWorldApp(.class).
I used both with class and without class. I got the same following
message.
Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorldApp(/class)
I was able to compile and run in the Linux machine at the University.
So, seems like problem is in my laptop envirionment.
Thank you very much.
Gordon Beaton - 30 Sep 2005 16:35 GMT
> I used both with class and without class. I got the same following
> message.
*Without* is the right one. java wants the name of the class, not the
name of the class file.
> Exception in thread "main" java.lang.NoClassDefFoundError:
> HelloWorldApp(/class)
>
> I was able to compile and run in the Linux machine at the University.
> So, seems like problem is in my laptop envirionment.
If you're in the same directory as the class file, try it like this:
java -cp . HelloWorldApp
(I'm assuming you've correctly compiled with javac prior to this)
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
thickface - 30 Sep 2005 16:53 GMT
Thanks. That works!
I would really appreciate it if you would explain why this happens and
how I prevent this from happening again.
Gordon Beaton - 30 Sep 2005 17:02 GMT
> Thanks. That works!
> I would really appreciate it if you would explain why this happens and
> how I prevent this from happening again.
Java searches a "classpath" to find the classes it needs. It's either
specified on the command line with -cp, or in an environment variable
CLASSPATH.
Apparently the current directory (".") wasn't mentioned.
More information is here:
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html
http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 30 Sep 2005 19:03 GMT
>Thanks. That works!
>I would really appreciate it if you would explain why this happens and
>how I prevent this from happening again.
see http://mindprod.com/jgloss/classpath.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 30 Sep 2005 19:00 GMT
>java.lang.NoClassDefFoundError
see http://mindprod.com/jgloss/caq.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 30 Sep 2005 19:02 GMT
>I am a newbie to Java. I just installed Java on my laptop and ran some
>example programs. And I got the message that says Exception in thread
>"main" java.lang.NoClassDefFoundError even though this example program
>is very simple and it's from a book. I would appreciate it if you give
>me some hints why this happens.
You might just back up and try another example. Tracking that error
is the most difficult of all Java errors.
One thing you want to find out is:
Is the problem associated with this particular example or every single
java program you try.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 30 Sep 2005 19:03 GMT
> java.lang.NoClassDefFoundError
if you are doing HelloWorld see
http://mindprod.com/jgloss/helloworld.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.