Java .jar 错误 - 无法找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21650851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.jar error - could not find or load main class
提问by Blrp
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:
我试图将 HelloWorld 放在一个 .jar 文件中并运行它,但它不起作用。我创建了java文件并在程序中输入,然后在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.
当我尝试使用 64 位 jre7\bin、jdk1.7.0_51\bin、jdk1.7.0_51\jre\bin 中的 java.exe 文件打开它时,我得到的输出(在快速关闭的窗口中)相同,如以及 32 位 jre7\bin。我已经卸载并重新安装了我的 jre 和 jdk,并重新创建了我的 .class 和 .jar 文件,但问题仍然存在。我是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
编辑:我试着按照 aetheria 的建议去做,但没有运气。我把HelloWorld.java放在path\com\stackoverflow\user\blrp中,编译,输入
java com.stackoverflow.user.blrp.HelloWorld
in path. I then created the manifest and jar by:
在路径中。然后我通过以下方式创建了清单和 jar:
(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.
但是, java -jar HelloWorld.jar 仍然有效,而 HelloWorld.jar 没有(相同的错误)。我也尝试不做包的事情,只做清单中的类路径,结果相同。
(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.)
(另外,我在使用 .bat 文件提出问题之前解决了这个问题,但让那个 jar 工作仍然很好。)
采纳答案by Blrp
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
感谢 jbaliuka 的建议。我打开了注册表编辑器(通过在 cmd 中键入 regedit)并转到 HKEY_CLASSES_ROOT > jarfile > shell > open > command,然后打开(默认)并更改值
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
to
到
"C:\Program Files\Java\jre7\bin\java.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.
(我刚刚删除了 javaw.exe 中的 w。)之后,您必须右键单击一个 jar -> 打开方式 -> 选择默认程序 -> 导航到您的 java 文件夹并打开 \jre7\bin\java.exe(或任何java文件夹中的其他java.exe文件)。如果它不起作用,请尝试切换到 javaw.exe,用它打开一个 jar 文件,然后切换回来。
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).
我对编辑注册表一无所知,只是它很危险,因此您可能希望在执行此操作之前对其进行备份(在顶部栏中,文件>导出)。
回答by ???v?т?
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.
-cp HelloWorld.jar
将 jar 添加到类路径,然后HelloWorld
运行您编写的类。
To create a runnable jar with a main class with no package, add Class-Path: .
to the manifest:
要使用没有包的主类创建可运行的 jar,请添加Class-Path: .
到清单中:
Manifest-Version: 1.0
Class-Path: .
Main-Class: HelloWorld
I would advise using a package
to give your class its own namespace. E.g.
我建议使用 apackage
为您的类提供自己的命名空间。例如
package com.stackoverflow.user.blrp;
public class HelloWorld {
...
}
回答by EuroTech
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.
我在寻找上述问题的答案时发现了这个问题。但就我而言,问题是使用“破折号”而不是“破折号”。检查您使用的是哪个破折号,它可能是错误的。我希望这个答案可以加快其他人的搜索速度,这样的评论可以为我节省一些时间。
回答by MoHaN K RaJ
I Faced the same issue while installing a setup using a jar file. Solution thta worked for me is
我在使用 jar 文件安装设置时遇到了同样的问题。对我有用的解决方案是
- open command prompt as administrator
- Go to jdk bin directory (Ex.C:\Program Files\Java\jdk1.8.0_73\bin)
- now execute
java -jar <<jar fully qualified path>>
- 以管理员身份打开命令提示符
- 进入 jdk bin 目录 (Ex.C:\Program Files\Java\jdk1.8.0_73\bin)
- 现在执行
java -jar <<jar fully qualified path>>
It worked for me :)
它对我有用:)
回答by wintoch
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.
如果这个问题找不到答案,所以我去寻找其他线程,我发现我正在使用 1.8 制作我的应用程序,但由于某种原因我的 jre 已经过时了,即使我记得更新它。我下载了 lastes jre 8 并且 jar 文件运行完美。希望这可以帮助。