Having Problems Running my Java Progs

Hey guys,

I keep getting the following error message when I try to run any of my java programs.

Exception in thread "main"

java.lang.NoClassDefFoundError: HelloApp

this is the app that im trying to run;

publicclass HelloApp

{

publicstaticvoid main (String[] args)

{

System.out.println("Hello, World");

}

}

I cant see anything wrong with this piece of code. Ive definately saved the code as HelloApp.java. I've tried several different programs all with the same result. Does anyone have any ideas on why this is happening?

Thank You

Jaz

[925 byte] By [Jazmana] at [2007-10-3 9:51:04]
# 1

Have you compiled it?

If not, you need to type

javac HelloApp.java

and press enter.

this will produce a class file named HelloApp.class

You can then run it by typing

java HelloApp

and pressing enter.

Now, if you were already compiling it, then you may have a classpath issue.

BobCa at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 2
Yes I have compiled it and I have the class file to prove it. As far as the class path problem is concerned, i've just checked now and I can see that I've pointed the path, in the environment variables, to the bin directory in the jdk folder.
Jazmana at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 3
yes, but you need to set the CLASSPATH variable to the location where you are executing your .Class files. I.e. CLASSPATH=C:\java\classes;etc.Or, when you execute the program, use the -cp argument:java -cp . HelloAppwhere the . indicates the current directory.
Navy_Codera at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 4

OK, classpath is different from path, it's specifically used by java to locate .class files.

Apparently some Windoze versions do not automatically include the local directory in the classpath - or may not have a classpath variable set until you do it manually.

So, the easiest way to see if this is your problem is to set the classpath in the command line like so:

java -cp C:\WhereIHidMyFiles HelloApp

(obviously, substitute the correct directory name above)

BobCa at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 5
Say for example my program was compiled and the resulting class file was saved to the c:\ drive. Then to run my java prog, I would have to type in the following in the command prompt;java -cp c:\HelloAppMessage was edited by: Jazman
Jazmana at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 6
Almost.java -cp c:\ HelloAppMake sure that there is a space between the backslash and the classname.
Navy_Codera at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 7

I have tried exactly as you have written (obviously substituting for my own directory) and I get the following message;

Usage: java [-options] class [args...]

(to execute a class)

or java [-options] -jar jarfile [args...]

(to execute a jar file)

and then a load of options. Am I missing an argument or something?

Jazmana at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 8
AHHHH yes! It works now!!!! Thank You. How would I change this permanantly so I dont have to type -cp all the time?
Jazmana at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...
# 9
You could create a windows environment variable named CLASSPATH. However, keep in mind that this value will be used as the default of all of your java programs. I would recommend just typing it out longhand until you learn how to create executable jar files.
bckrispia at 2007-7-15 5:08:21 > top of Java-index,Java Essentials,New To Java...