“找不到主类”双击 .jar 文件时

发布于 2024-10-26 20:55:24 字数 521 浏览 9 评论 0原文

首先:我意识到这个问题应该很简单,而且很多人似乎都经历过。但是,看来我的问题可能与现有帖子略有不同。

我正在使用 NetBeans 编译 Java 应用程序。构建完成后,输出将正确进入 dist\ 文件夹。 jar 文件就在那里。如果我进入命令行,导航到该文件夹​​,然后调用以下命令:

java -jar Prosperity.jar

一切正常。显然,该应用程序的名称是“繁荣”。以下命令也可以正常工作:

javaw -jar Prosperity.jar

但是,双击 .jar 文件会出现消息:“无法找到主类:”,然后给出 .jar 文件的路径。我已经检查了一百次,确认 .jar 文件中存在 META-INF 文件夹,并且其中存在具有正确主类名的 MANIFEST.MF。我还检查了主类(App.class).class 文件是否存在于.jar 文件中。

所以问题是:双击文件和在其上运行 javaw 有什么区别?它无法运行,这真是令人沮丧!

感谢您给我的任何指示,我在这里撕扯我的头发!

First: I realize that this issue should be quite simple and lots of people seem to have experienced it. However, it appears my issue may be slightly different from existing posts.

I'm using NetBeans to compile a Java application. When it's done building, the output goes into the dist\ folder correctly. The jar file is present there. If I go to the command line, navigate to that folder, and call the following:

java -jar Prosperity.jar

everything works fine. Obviously the name of the application is Prosperity. The following command also works fine:

javaw -jar Prosperity.jar

However, double-clicking the .jar file gives the message: "Could not find the main class:", and then gives the path to the .jar file. I've checked a hundred times that the META-INF folder exists inside the .jar file, and that within it there exists a MANIFEST.MF with the correct main class name. I have also checked that the main class (App.class) .class file exists inside the .jar file.

So the question is: what's the difference between double-clicking a file and running javaw on it? It's getting really frustrating that it won't just run!

Thanks for any direction you can give me, I'm tearin' my hair out here!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

戏蝶舞 2024-11-02 20:55:24

也许你的文件关联被搞乱了。在命令提示符处,尝试运行

ftype | find "jarfile"

On my 64-bit Windows 7 computer,这显示

jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

You can also change it with ftype

ftype jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

Perhaps your file associations got messed up. At the command prompt, try running

ftype | find "jarfile"

On my 64-bit Windows 7 computer, that shows

jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

You can also change it with ftype:

ftype jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*
瞄了个咪的 2024-11-02 20:55:24

刚刚找到这篇文章...

如果您仅在双击 jar 文件时出现问题,而不是在 cmd 启动时遇到问题,则可能是因为 JRE 的版本错误(6 代替 7)。

只需更改 regedit 中的值:

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\shell\open\command]
"C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

到:

"C:\Program Files\Java\jre7\bin\ javaw.exe" -jar "%1" %*

如果更新这个值应该没有问题。


注意:在 Windows 上切换 32 位和 64 位版本:

-> "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* 将使用 64 位版本的 JRE
-> "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %* 将使用 32 位版本的 JRE

Just found this post...

If you have the problem only when double clicking the jar file and not at cmd launch, it's probably because the version of the JRE is wrong (6 in place of 7).

Just change the value in regedit at :

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\shell\open\command]
"C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

to :

"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*

There should be no problem if this value is updated.


Note : Switching between 32 and 64 bits versions on Windows :

-> "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* will use the 64 bits version of the JRE
-> "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %* will use the 32 bits version of the JRE

任谁 2024-11-02 20:55:24

搜索您的注册表并检查以下项目

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Applications\javaw.exe\shell\open\command]
@="\"C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\""


[HKEY_CLASSES_ROOT\jarfile\shell\open\command]
@="\"C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\""


[HKEY_CURRENT_USER\Software\Classes\Applications\javaw.exe\shell\open\command]
@="\"C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\""

search your regedit and check follow item

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Applications\javaw.exe\shell\open\command]
@="\"C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\""


[HKEY_CLASSES_ROOT\jarfile\shell\open\command]
@="\"C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\""


[HKEY_CURRENT_USER\Software\Classes\Applications\javaw.exe\shell\open\command]
@="\"C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\""
荒岛晴空 2024-11-02 20:55:24

我也遇到过这个错误。我使用命令提示符检查了文件关联,一切都是正确的。直到我尝试使用 java -jar MyProgram.jar 从命令行运行 .jar 时,它才真正向我展示了问题的根源。

事实证明 .jar 是在 JDK 7 下编译的,而我只运行 JRE 6。这是提示中给我的错误,它引导我找到了真正的解决方案:

Exception in thread "main" java.lang.UnsupportedClassVersionError: MyProgram/Program : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MyProgram.Program. Program will ex
it.

I came across this error as well. I checked the file association using command prompt and all was correct. It wasn't until I tried running the .jar from the command line using java -jar MyProgram.jar that it actually showed me the root of the problem.

It turns out the .jar was compiled under JDK 7 whereas I was only running JRE 6. This was error given to me in the prompt which lead me to the real solution:

Exception in thread "main" java.lang.UnsupportedClassVersionError: MyProgram/Program : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MyProgram.Program. Program will ex
it.
眼波传意 2024-11-02 20:55:24

我刚刚遇到了完全相同的问题,以相同的方式解决了。我必须完全删除关联,而不仅仅是用户自定义,然后重新安装 JRE。

修复前的关联仍然是:

jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

修复后(相同):

jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

I just had the exact same problem, fixed the same way. I had to completely delete the association, not just the user customisation, and reinstall the JRE.

The association before the fix was still:

jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*

And after (the same):

jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*
少女的英雄梦 2024-11-02 20:55:24

如果上述方法不能解决问题,请尝试从电脑中删除.jar文件类型(可以google一个名为Unassoc.exe的软件,删除该文件类型),然后问题就解决了! (至少,这是我的情况!)

If the methods above cannot solve the problem, try deleting the .jar file type from your pc (you can google a software called Unassoc.exe and delete the file type), and then the problem is fixed! (At least, this is my case!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文