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

查看:103
本文介绍了.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

并且有效。然后我输入

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 path\HelloWorld.jar

当我尝试用64位jre7 \ bin中的java.exe文件打开它时,我得到的输出(在一个快速关闭的窗口中) ,jdk1.7.0_51 \ bin,jdk1.7.0_51 \ jre \ bin,以及32位jre7 \ bin。我已经卸载并重新安装了我的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 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.

编辑:我试图像aetheria那样建议,但没有运气。我把HelloWorld.java放在path \comcom \stackoverflow \ user \ blrp中,编译它,然后输入

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

在路径中。然后我创建了清单和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 com\stackoverflow\user\blrp\HelloWorld.class

并获得输出

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

但是,java -jar HelloWorld.jar工作且HelloWorld.jar没有(同样的错误)。我也尝试过不做包装的东西,只是清单中的Class-Path,结果相同。

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>命令,然后打开(默认)并更改值

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

(我刚删除了javaw.exe中的w。)之后你必须要正确单击一个jar - >打开 - >选择默认程序 - >导航到您的java文件夹并打开\ jre7\bin\java.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 \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).

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

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