I tried to put HelloWorld in a .jar file and running it, but it doesn't work. I created the java file and typed in the program, and then wrote in cmd:
javac HelloWorld.java java HelloWorld
and it worked. Then I entered
echo Main-Class: HelloWorld >manifest.txt jar cvfm HelloWorld.jar manifest.txt HelloWorld.class
and got the output
added manifest adding: HelloWorld.class(in = 426) (out= 288)(deflated 32%)
I then entered
java -jar HelloWorld.jar HelloWorld.jar
and the first line worked, while the second line gave me an error:
Error: Could not find or load main class path\HelloWorld.jar
which is the same output I got (in a rapidly closing window) when I tried to open it with the java.exe file in 64 bit jre7\bin, jdk1.7.0_51\bin, jdk1.7.0_51\jre\bin, as well as 32 bit jre7\bin. I've uninstalled and reinstalled both my jre and jdk and recreated my .class and .jar files, but the problem persists. I'm on win8.
Edit: I tried to do as aetheria suggested, but no luck. I put HelloWorld.java in path\com\stackoverflow\user\blrp, compiled it, and it worked by entering
java com.stackoverflow.user.blrp.HelloWorld
in path. I then created the manifest and jar by:
(echo Manifest-Version: 1.0 echo Class-Path: . echo Main-Class: com.stackoverflow.user.blrp.HelloWorld) >manifest.txt jar cvfm HelloWorld.jar manifest.txt com\stackoverflow\user\blrp\HelloWorld.class
and got the output
added manifest adding: com/stackoverflow/user/blrp/HelloWorld.class(in = 454) (out= 312)(deflat ed 31%)
but still, java -jar HelloWorld.jar worked and HelloWorld.jar didn't (same error). I also tried not doing the package thing, just the Class-Path in manifest, same result.
(Also, I solved the problem prior to asking the question by use of a .bat file, but it'd still be sweet to get that jar working.)
When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.
Sometimes due to out of memory space error, NetBeans is unable to load or find the main class. RightClick on the project node and go to Set configuration. Select the main class for your application. Then clean and build.
Thanks jbaliuka for the suggestion. I opened the registry editor (by typing regedit in cmd) and going to HKEY_CLASSES_ROOT > jarfile > shell > open > command, then opening (Default) and changing the value from
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
to
"C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
(I just removed the w in javaw.exe.) After that you have to right click a jar -> open with -> choose default program -> navigate to your java folder and open \jre7\bin\java.exe (or any other java.exe file in you java folder). If it doesn't work, try switching to javaw.exe, open a jar file with it, then switch back.
I don't know anything about editing the registry except that it's dangerous, so you might wanna back it up before doing this (in the top bar, File>Export).
I found this question when I was looking for the answer to the above question. But in my case the issue was the use of an 'en dash' rather than a 'dash'. Check which dash you are using, it might be the wrong one. I hope this answer speeds up someone else's search, a comment like this could have saved me a bit of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With