Blrp Blrp - 2 years ago 462
Java Question

.jar error - could not find or load main class

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:

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

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 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.)

Answer Source

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" %*


"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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download