.jar 错误 - 无法找到或加载主类 [英] .jar error - could not find or load main class

查看:38
本文介绍了.jar 错误 - 无法找到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 HelloWorld 放在 .jar 文件中并运行它,但它不起作用.我创建了java文件并输入程序,然后在cmd中写入:

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

并得到输出

added manifest
adding: HelloWorld.class(in = 426) (out= 288)(deflated 32%)

然后我输入了

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 pathHelloWorld.jar

当我尝试使用 64 位 jre7in、jdk1.7.0_51in、jdk1.7.0_51jre 中的 java.exe 文件打开它时,我得到的输出相同(在快速关闭的窗口中)bin,以及 32 位 jre7in.我已经卸载并重新安装了我的 jre 和 jdk,并重新创建了我的 .class 和 .jar 文件,但问题仍然存在.我用的是 win8.

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 jre7in, jdk1.7.0_51in, jdk1.7.0_51jrein, as well as 32 bit jre7in. 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.

我试着按照 aetheria 的建议去做,但没有运气.我把HelloWorld.java放在pathcomstackoverflowuserlrp,编译,输入

I tried to do as aetheria suggested, but no luck. I put HelloWorld.java in pathcomstackoverflowuserlrp, compiled it, and it worked by entering

java com.stackoverflow.user.blrp.HelloWorld

在路径中.然后我通过以下方式创建了清单和 jar:

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 comstackoverflowuserlrpHelloWorld.class

并得到输出

added manifest
adding: com/stackoverflow/user/blrp/HelloWorld.class(in = 454) (out= 312)(deflat
ed 31%)

但是,java -jar HelloWorld.jar 仍然有效,而 HelloWorld.jar 没有(同样的错误).我也试过不做包的事情,只做清单中的类路径,结果一样.

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.

(另外,我在使用 .bat 文件提出问题之前解决了这个问题,但是让那个 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.)

推荐答案

感谢 jbaliuka 的建议.我打开了注册表编辑器(通过在 cmd 中键入 regedit)并转到 HKEY_CLASSES_ROOT > jarfile > shell > open > command,然后打开(默认)并更改值

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 FilesJavajre7injavaw.exe" -jar "%1" %*

"C:Program FilesJavajre7injava.exe" -jar "%1" %*

(我刚刚删除了 javaw.exe 中的 w.)之后,您必须右键单击一个 jar -> 打开方式 -> 选择默认程序 -> 导航到您的 java 文件夹并打开 jre7injava.exe(或您的 java 文件夹中的任何其他 java.exe 文件).如果它不起作用,请尝试切换到 javaw.exe,用它打开一个 jar 文件,然后切换回来.

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

这篇关于.jar 错误 - 无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆