Click to See Complete Forum and Search --> : running java problem


monkeyboi
09-10-2002, 03:38 AM
i hav been playing around with alot of computer language and now it's java's turn...

anyway java is pretty similar to C so looked at some example and start writing my first java program..

ok i compiled using javac, compilation was a success. now i hav a binary file first.class...

running my first java program...

java first

bash-2.05a$ java test.class
Exception in thread "main" java.lang.NoClassDefFoundError: test/class
bash-2.05a$

and it gave me that error.

so i boot to windoze and compile a fresh java program for windoze, same error as slackware... however it only work in windoze when i move the first.class into the bin dir inside the directory where java was installed....

any ideas how to fix this problem??
thx

monkeyboi
09-10-2002, 03:40 AM
btw

bash-2.05a$ java first.class
Exception in thread "main" java.lang.NoClassDefFoundError: test/class
bash-2.05a$

not test.class
hehehe

mingshun
09-10-2002, 04:53 AM
Originally posted by monkeyboi
btw

bash-2.05a$ java first.class
Exception in thread "main" java.lang.NoClassDefFoundError: test/class


I think it should be
$ java first

(Don't supply the full class filename)

monkeyboi
09-10-2002, 08:00 AM
try that too...
same error

truls
09-10-2002, 08:35 AM
The class containing the main method must have the same name as the file. I guess your java class is called test while your java file is called first.java. These must match( class test must be in a file called test.java ).

monkeyboi
09-10-2002, 12:25 PM
i kno, i got mix up in the question.
but my file name is the same as the class name...

btw applet works fine

mingshun
09-10-2002, 06:48 PM
Originally posted by monkeyboi
i kno, i got mix up in the question.
but my file name is the same as the class name...

btw applet works fine

Are you trying to run a applet by calling the java interpreter? i.e: java applet

Do note take this will NOT work because as the error already tells you, you need a "public static void main (String args[])" in order to do that.

monkeyboi
09-11-2002, 11:09 PM
i mean when i write a java applet, compiled, and use appletviewer the run the applet, it works fine...

i only hav problem run commandline java program....

Sawdusty
09-11-2002, 11:29 PM
what jre are you using?

what does java -version give you?

If the code compiled properly with javac, then you probably don't have a problem there.

Are you in the directory you're trying to run the program from? If so, is . in your classpath?

echo $CLASSPATH

will tell you what the classpath is. The first entry should be a .

export CLASSPATH=.:CLASSPATH

will add the working directory to your CLASSPATH

hth.

Dusty

mingshun
09-12-2002, 12:04 AM
Originally posted by monkeyboi
i mean when i write a java applet, compiled, and use appletviewer the run the applet, it works fine...

i only hav problem run commandline java program....

I know what you mean.
My question was did your applet include this line:
"public static void main (String args[])". If not, "java <applet_name>" will not work.

monkeyboi
09-12-2002, 12:40 AM
yes it does has that line

mingshun
09-12-2002, 01:36 AM
Originally posted by monkeyboi
yes it does has that line

From my experience:

1. Your classpath is not set properly. So follow what Sawdusty said.

2. Try recompling your *.java

appletviewer works because the html code contains the codebase that already tells it where the *.classes are. But the command "java" doesn't. This is something like running "a.out" with the not found error.