[java] Could not find or load main class

I have Windows 7, installed jdk1.7.0 and its supporting jre7.
My problem is compilation part works perfectly, but while running the Java program I get this error saying:

"Could not find or load main class"

I am storing all my programs in javalab folder. I have set the path to it. Procedure looks like this:

C:\Users\user>cd\

C:\>cd javalab

C:\javalab>autoexec.bat

C:\javalab>set path=C:\Program Files\Java\jdk1.7.0\bin

C:\javalab>javac p1.java

C:\javalab>java p1
Error: Could not find or load main class p1

C:\javalab> 

This question is related to java windows

The answer is


You might have the CLASSPATH environment variable already added!!

Use following to avoid further usage of -cp . in java -cp . CLASSFILE

Add . to CLASSPATH in system properties->environment variables or by cmd

set CLASSPATH=%CLASSPATH%;.;


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?


Try:

java -cp . p1

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


i guess that you have a different class name in p1.java


You can use NetBeans IDE which is free to download and use "Open Source". You can even do other programming language in this IDE. The latest of which it supports HTML5. This makes your programming easier. If you're not familiar with it choose a book that is NetBeans integrated like Sams Teach Yourself Java in 24 Hours


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.


Here is my working env path variables after much troubleshooting

CLASSPATH

.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files (x86)\Java\jdk1.6.0_27\bin

PATH <---sometimes this PATH fills up with too many paths and you can't add a path(which was my case!)

bunchofpaths;C:\Program Files (x86)\Java\jdk1.6.0_27\bin

Additionally, when you try to use the cmd to execute the file...make sure your in the local directory as the file your trying to execute (which you did.)

Just a little checklist for people that have this problem still.


First, put your file *.class (e.g Hello.class) into 1 folder (e.g C:\java). Then you try command and type cd /d C:\java. Now you can type "java Hello" !


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

If you have a single .java file to compile using command-line , then remove topmost package parts from the code, the compile again, it will work.

This worked for me.


i had

':'

in my project name e.g 'HKUSTx:part-2' renaming it 'HKUSTx-part-2' worked for me


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

Check you class name first. It should be p1 as per your batch file instruction. And then check you package of that class, if it is inside any package, specify when you run.

If package is x.y

java x.y.p1

I've had similar problems. If you work with Eclipse, you need to go to the folder where you have your src/ folder... If you used a package - then you use

javac -cp . packageName/className

which means if you've had a package named def and main class with name TextFrame.java you'd write

javac -cp . def/TextFrame

omitting the trailing .java extension, and then you run it with the

java def/TextFrame 

and if you have have arguments, then you need to supply it with arguments corresponding to your program. I hope this helps a bit.


I faced a similar problem in Eclipse. Whenever I clicked on the Run button it gave me the message, "Error: Could not find or load main class". But when I right click on the java file in the project explorer and Run As Java configuration, it works perfectly.

I think this is because it tries by default to run it in some other configuration which causes problems.

Hope this answer helps some.


Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:

Could not find or load main class xxx Linux

I just delete that reference and added again and it worked fine again.