.jar error - could not find or load main class

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

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

回答1:

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



回答2:

You can always run this:

java -cp HelloWorld.jar HelloWorld 

-cp HelloWorld.jar adds the jar to the classpath, then HelloWorld runs the class you wrote.

To create a runnable jar with a main class with no package, add Class-Path: . to the manifest:

Manifest-Version: 1.0 Class-Path: . Main-Class: HelloWorld 

I would advise using a package to give your class its own namespace. E.g.

package com.stackoverflow.user.blrp;  public class HelloWorld {     ... } 


回答3:

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.



回答4:

Had this problem couldn't find the answer so i went looking on other threads, I found that i was making my app with 1.8 but for some reason my jre was out dated even though i remember updating it. I downloaded the lastes jre 8 and the jar file runs perfectly. Hope this helps.



回答5:

I Faced the same issue while installing a setup using a jar file. Solution thta worked for me is

  1. open command prompt as administrator
  2. Go to jdk bin directory (Ex.C:\Program Files\Java\jdk1.8.0_73\bin)
  3. now execute java -jar >

It worked for me :)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!