“Could not find the main class” when double-clicking .jar file

时间:2021-09-14 20:44:56

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:

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

java -jar Prosperity.jar

java -jar Prosperity.jar

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

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

javaw -jar Prosperity.jar

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.

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

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!

所以问题是:双击文件和运行javaw之间的区别是什么?它真的很令人沮丧,它不会只是运行!

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

谢谢你能给我的任何方向,我是tearin'我的头发在这里!

6 个解决方案

#1


24  

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

在我的64位Windows 7计算机上,显示

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

You can also change it with ftype:

您也可以使用ftype更改它:

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

#2


9  

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

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

Just change the value in regedit at :

只需更改regedit中的值:

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

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

“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 :

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

-> "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

- >“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

#3


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\""

#4


1  

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.

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

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:

事实证明.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.

#5


0  

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.

我只是遇到了完全相同的问题,修复方法相同。我不得不完全删除关联,而不仅仅是用户自定义,并重新安装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" %*

#6


0  

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

如果上述方法无法解决问题,请尝试从您的电脑中删除.jar文件类型(您可以谷歌一个名为Unassoc.exe的软件并删除文件类型),然后问题得到解决! (至少,这是我的情况!)

#1


24  

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

在我的64位Windows 7计算机上,显示

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

You can also change it with ftype:

您也可以使用ftype更改它:

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

#2


9  

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

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

Just change the value in regedit at :

只需更改regedit中的值:

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

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

“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 :

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

-> "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

- >“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

#3


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\""

#4


1  

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.

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

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:

事实证明.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.

#5


0  

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.

我只是遇到了完全相同的问题,修复方法相同。我不得不完全删除关联,而不仅仅是用户自定义,并重新安装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" %*

#6


0  

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

如果上述方法无法解决问题,请尝试从您的电脑中删除.jar文件类型(您可以谷歌一个名为Unassoc.exe的软件并删除文件类型),然后问题得到解决! (至少,这是我的情况!)