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:
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
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.
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.
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
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?
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