Reputation: 241
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>
Upvotes: 24
Views: 163839
Reputation: 95
i had
':'
in my project name e.g 'HKUSTx:part-2' renaming it 'HKUSTx-part-2' worked for me
Upvotes: 0
Reputation: 183
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.
Upvotes: 1
Reputation: 1167
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
Upvotes: 9
Reputation: 1498
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.
Upvotes: 1
Reputation: 13910
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.
Upvotes: 0
Reputation: 7
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
Upvotes: -2
Reputation: 27
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%;.;
Upvotes: 1
Reputation: 1724
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.
Upvotes: 2
Reputation: 1906
I had almost the same problem, but with the following variation:
That's quite odd behavour, I cannot completely understand it. Hope It'll help somebody. too.
Upvotes: 4
Reputation: 312
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.
Upvotes: 3
Reputation: 1467
Try:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux)
Upvotes: 12
Reputation: 331
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
Upvotes: 28
Reputation: 11
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" !
Upvotes: 1
Reputation: 4945
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
Upvotes: 3
Reputation: 28178
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?
Upvotes: 6
Reputation: 63698
i guess that you have a different class name in p1.java
Upvotes: 3