Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Could not find the main class error  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott McWilliams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott McWilliams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!