Could not find main class HelloWorld
Solution 1:
Tell it where to look for you class: it's in ".", which is the current directory:
java -classpath . HelloWorld
No need to set JAVA_HOME
or CLASSPATH
in this case
Solution 2:
You are not setting a classpath that includes your compiled class! java
can't find any classes if you don't tell it where to look.
java -cp [compiler outpur dir] HelloWorld
Incidentally you do not need to set CLASSPATH the way you have done.
Solution 3:
Just remove your "classpath" from you environment variable. Then try running:
java HelloWorld
This should work fine.
Solution 4:
Java is not finding where your compiled class file (HelloWorld.class) is. It uses the directories and JAR-files in the CLASSPATH
environment variable for searching if no -cp
or -classpath
option is given when running java.exe.
You don't need the rt.jar in the CLASSPATH
, these was only needed for older versions of Java. You can leave it undefined and the current working directory will be used, or just add .
(a single point), separated by ';', to the CLASSPATH
variable to indicate the current directory:
CLASSPATH: .;C:\...\some.jar
Alternatively you can use the -cp
or -classpath
option:
java -cp . HelloWorld
And, as Andreas wrote, JAVA_HOME
is not needed by Java, just for some third-party tools like ant (but should point to the correct location).