views:

198

answers:

6

I have just installed JDK on Windows Vista. After that I set proper values for the 4 environment variables: classpath, include, lib, path. After that I was able to compile my HelloWorld-program (I got a *.class file). But when I try to execute the compiled program (I type java HelloWorldApp) it does not work. The Java write a lot of stuff and in the end it is written that it "could not find the main class: HelloWorldApp". Can anybody, pleas, help me with this problem?

A: 

Have you included . and .. in your path? Just for clarification . represents your current directory and .. represents your parent directory. You are telling that the java has to search the current directory and the parent directory to find the class. Add the same to your classpath too.

Teja Kantamneni
+5  A: 

Just for clarity; you are saying that you have a class in the default package, that is you have not included a package specifier in the Java file, and your class is called HelloWorldApp. When you compiled this, you got a classfile HelloWorldApp.class in the current directory.

Assuming the above to be true then try:

java -cp . HelloWorldApp

For example, the following works on a unix box:

$ echo 'class HelloWorldApp { public static void main(String []argv) { System.out.println("Hello World!"); } }' > HelloWorldApp.java
$ javac HelloWorldApp.java 
$ java -cp . HelloWorldApp 
Hello World!

Of course, you should indent your code a little nicer than just shoving the whole thing onto one line ;-)

Edit: To answer the comment:

Normally, the default classpath is the runtime libraries and the current directory. However, if you have the CLASSPATH variable set, then this will override the default, and you need to explicitly set the classpath back to its "default value". To verify if the CLASSPATH environment variable is set, you can do (again assuming unix):

set | grep CLASSPATH

If it is set, that is why you need to manually include . on your classpath.

Paul Wagland
OK. It works! If I type "java -cp . HelloWorldApp" I get the "Hello World!". But I still need to figure out why.
Roman
@Roman, the *why* can be found here: http://en.wikipedia.org/wiki/Classpath_%28Java%29
Bart Kiers
@Bart: That is the wrong why ;-) I have updated my answer to explain the most probable cause. Certainly, on my box I do not _need_ to specify the classpath, and it still works.
Paul Wagland
Perhaps it is, perhaps not. By Roman's remark "why" it works (or why not), I guessed Roman was unfamiliar what happens behind the scenes when a Java application is being executed. I think the WIki article gives a good overview of this process (also see the links at the bottom of that page). Also note that you need not assume Roman is running a *nix like OS (see the first sentence of the original question) :)
Bart Kiers
@Bart: You are right... however, I don't have a Windows system to test on :-). My wording is probably a little strong, certainly I think the problem is likely to be a globally set CLASSPATH, but I cannot be certain of that…
Paul Wagland
Yes, I share your belief about the globally set CLASSPATH (which should be banned, btw!) :)
Bart Kiers
A: 

What happens if you use:

java -cp {path to directory with HelloWorldApp in it} HelloWorldApp

That path should be contained within your CLASSPATH environment variable. Is that exported to your command shell ? Do you need to start a new command shell to get the most recent version of CLASSPATH ?

Brian Agnew
A: 

Post your code. I believe the problem is that your main class is not defined properly. I did this the other day.

public static void main(String[] args){
    //code
}
piggles
+3  A: 
  1. create a file called HelloWorld.java;
  2. paste the code posted below inside HelloWorld.java:
  3. compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
  4. execute the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("HelloWorld works!");
    }
}

How the classpath works, can be read here: http://en.wikipedia.org/wiki/Classpath_%28Java%29

Bart Kiers
BTW, that wikipedia page is not entirely correct for the Sun JDK. On the Sun JDK, the default classpath includes the current directory, unless it is overridden. <http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/classpath.html> for details.
Paul Wagland
I must confess that I did not read the entire Wiki-entry, but if parts of it are incorrect (not sure by your comment which part you mean...) you could perhaps edit it (that's what Wikipedia is all about, right?).
Bart Kiers
A: 

The class path concept and the logical difference between Java source code and compiled byte code is notoriously hard to get right.

I would strongly recommend you familiarize yourself with the Sun Java Tutorial. The relevant section is

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Thorbjørn Ravn Andersen