Roman Roman - 5 months ago 11
Java Question

I have managed to compile java-program but I cannot execute it

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?

Answer
  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