Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 840
aardvarkk Avatar asked Mar 25 '11 02:03

aardvarkk


People also ask

How do I find the main class in a JAR file?

Technically a jar file can contain more than one main class. When java executes a jar file, it looks in the META-INF/MANIFEST. MF file inside the jar to find the entrypoint. There is no direct command to get this information, but you can unpack the jar (it's just a zip file) and look into the manifest yourself.

How do I run a .JAR file by double click?

Normally after installing Java, there will be an association between . jar and the javaw.exe. This enables the executable jar to be opened on double click. You can check the association by going to Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program.

Does a JAR file need a main method?

For a JAR file to run, the JAR itself must contain at least one class that has a runnable main method. Furthermore, the class with the main method must be listed as the main-class in the JAR's manifest file.

Can a jar have two main classes?

Jar files can contain only one Main-Class attribute in the manifest, which means a jar can contain only one mainClassName.


2 Answers

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" %* 
like image 91
Jordan Miner Avatar answered Sep 24 '22 13:09

Jordan Miner


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

like image 24
tatactic Avatar answered Sep 21 '22 13:09

tatactic