Could not find or load main class

Solution 1:

I was having a similar issue with my very first java program.

I was issuing this command

java HelloWorld.class

Which resulted in the same error.

Turns out you need to exclude the .class

java HelloWorld

Solution 2:

Try:

java -cp . p1

This worked for me when I had the same problem, using Fedora (linux)

Solution 3:

Simple way to compile and execute java file.(HelloWorld.java doesn't includes any package)

set path="C:\Program Files (x86)\Java\jdk1.7.0_45\bin"
javac "HelloWorld.java"
java -cp . HelloWorld
pause

Solution 4:

javac should know where to search for classes. Try this:

javac -cp . p1.java

You shouldn't need to specify classpath. Are you sure the file p1.java exists?

Solution 5:

I had almost the same problem, but with the following variation:

  1. I've imported a ready-to-use maven project into Eclipse IDE from PC1 (project was working there perfectly) to another PC2
  2. when was trying to run the project on PC 2 got the same error "Could not find or load main class"
  3. I've checked PATH variable (it had many values in my case) and added JAVA_HOME variable (in my case it was JAVA_HOME = C:\Program Files\Java\jdk1.7.0_03) After restarting Ecplise it still didn't work
  4. I've tried to run simple HelloWorld.java on PC2 (in another project) - it worked
  5. So I've added HelloWorld class to the imported recently project, executed it there and - huh - my main class in that project started to run normally also.

That's quite odd behavour, I cannot completely understand it. Hope It'll help somebody. too.