Forums Register Login
Could not find the main class error
I am new to the world of Java programming. I downloaded the HelloWorldApp program from Oracle's website and saved it to c:\java , which is also where all my JDK and JRE files are. I ran javac and the program compiled to HelloWorldApp.class. I ran c:\java -cp . HelloWorldApp and I get the could not find the main class error. Note that I also used c:\set classpath= as has been suggested in other posts. I am stumped - can anyone help?
Hi Scott, and welcome to JavaRanch!

That is a bit perplexing. The code you compiled looks exactly like this? The only thing I can think of is you are somehow using different versions of Java for javac and java.

Try the commands:

java -version
javac -version

... and see if the versions match.
Thanks, Greg.

For Java: Version 1.6.0_22

For javac: version 1.7.0_25

They are different - is this what my problem is?
That will be a problem, because if you compile your classes with the compiler from JDK 7 they will not run on Java 6. You would get a different error for that though than "could not find the main class".

Make sure you set the PATH variable to include the JDK bin directory, as explained in the installation instructions for the JDK. The JDK bin directory should be in the PATH before any other directory that contains a java.exe. If you've done this correctly, your java and javac versions should match.

For your "could not find the main class" error: It's best to leave CLASSPATH not set. Java should take the current directory as the default classpath if you do that, so that you should just be able to do java HelloWorldApp to run the program (after you've compiled it, ofcourse).

Note: it's java HelloWorldApp, not java HelloWorldApp.class. The java command expects a class name, not a filename.

Also, I wouldn't save Java source files in the JDK directory. Create a separate directory somewhere else to store your own Java programs, don't mess with anything in the JDK directory.
I deleted Java 1.6 from my system and re-installed 1.7 (not sure why the old version isn't deleted automatically anyway). Apparently that did the trick. Thanks!

Scott McWilliams wrote:(not sure why the old version isn't deleted automatically anyway).

You can have multiple Java versions installed on your system, in fact I have JDK 5, 6, and 7 installed on my system (for testing my code with different versions). It would be very annoying if the installer for one JDK would automatically remove the older JDKs!
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 935 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 22:15:02.