Lang exception on linux
Hi, I'm still trying to run a Hello program in java that throws me the next exception:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
Hello is the name of the program, it has no sintax errors but Im not sure what is it that is
happening, hope anyone can help me... and merry Christmass : )
> Hi, I'm still trying to run a Hello program in java
> that throws me the next exception:
> Exception in thread "main"
> java.lang.NoClassDefFoundError: Hello
> Hello is the name of the program, it has no sintax
> errors but Im not sure what is it that is
> happening, hope anyone can help me... and merry
> Christmass : )
This ought to be a CLASSPATH problem.
You might try doing...
setenv CLASSPATH $CLASSPATH:.
before executing the program.
There are a couple of things that could cause this. First, have you created a .class file? You can follow the steps in this tutorial:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/unix.html
Next, make sure that you are using the version of Java that you think you are using. You can use the command "java -version" to check. The version number should be greater than 1.2. Some Linux distributions come with an ancient version of the runtime environment, 1.1.x, which has a distinct way of finding classes.
Well, first of all thanks for replying to my message, well I tried the setenv option
but seems like there isn't that command in slackware distribution.
I had no problem creating the class file using the ./javac command.
I'm using the Java 2 Runtime Enviroment,Standard Edition(Build 1.4.2_06-b03),
I downloaded it from the java site.
I almost forgot, when I trie to run the program I execute this instruction:
./java Hello.class
And the error is:Exception in thread "main" java.lang.NoClassDefFoundError: Hello/class
if I execute ./java Hello
The error is different:Exception in thread "main" java.lang.NoSuchMethodError: main
If your shell is of the csh family (csh, tcsh, I don't recall what else) then you'll have setenv. For Bourne shell family (sh, bash, zsh, ksh, ...) that doesn't exist. It would instead be:CLASSPATH=$CLASSPATH:.
I don't recommend that though. Better to just specify it in the command line with -classpath or -cp: java -cp . Hello
However, if you haven't explicitly set a classpath, it will implicitly just be the current dir anyway.
Also, you don't add ".class" to the command line.
So assuming you have Hello.class in the current directory, and that class isn't in a package (you don't have package com.mycompany.whatever; at the top of the .java file), then you'd run it like this: java Hello
For more details, follow the link jsalonen gave you, or google for java classpath.
> if I execute ./java Hello
> The error is different:Exception in thread "main"
> java.lang.NoSuchMethodError: main
You generally won't want your class files to be in the same directory as the java executable. However, going with what you have so far, it is finding Hello.class. Now the problem is it can't find the main method. Most likely you forgot the static modifier, but it could be some other typo. Make sure main's declaration looks exactly like this: public static void main(String[] args)
The NoClassDefFoundError is easily explained: the java launcher application expects to get the name of the class to run; not the name or path of the file.
You get a "NoSuchMethodError: main" if class you try to run does not have a method called main or if its declaration is wrong. The main method is used for starting the application, and typically is declared as "public static void main(String[] args)". Make sure you haven't made a typo or forgotten one of the keywords. You can see the code of a complete, working application here:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/unix.html#2a
You'll probably find this list of common error messages and their explanations useful:
http://mindprod.com/jgloss/errormessages.html
Sorry Jeff, didn't see you had said the same things before posting.
No problem. A second opinion never hurts.
Thanks to everyone, I could finally run my program with the -cp option.
> Thanks to everyone, I could finally run my program> with the -cp option.So give them the dukes already!
> > Thanks to everyone, I could finally run my program> > with the -cp option.> > So give them the dukes already!Another duke hungry person...
> > So give them the dukes already!> > Another duke hungry person...No, not I - was not even one of the people who answered. Just bugged by his "promise" to "pay" the dukes and doesn't go thru with it.