30

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!

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • 2
    It seems that somehow Java is getting the idea that my main class is actually named the PATH to the jar file that I'm running. No idea how it's coming up with that. It seems to be ignoring the value inside the manifest and looking for a main class with the name: C:\Users\MyUser\MyApp\Prosperity.jar. I'm assuming if I could figure out how that's becoming the name of the main class it's looking for, then I could solve the problem. – aardvarkk Mar 25 '11 at 03:15

6 Answers6

25

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" %*
Jordan Miner
  • 2,034
  • 17
  • 19
  • 1
    This was the ticket. To really get things movin', I downloaded a program called unassoc whose purpose is to delete all trails of file associations. Also of note was the fact that my application install was doing a *silent* install of the JRE, which (silently, of course!) *does not* associate .jar files correctly. So I had to change the main program install to no longer do a silent install, and also clean up my file associations. After reinstalling the JRE, I could execute the .jar file by double-clicking on it. Just wish I knew what the file association looked like when it was broken... – aardvarkk Mar 25 '11 at 04:34
  • Glad to see you got it working. I would assume the wrong association was missing the `-jar` parameter. That would cause the error you mentioned. – Jordan Miner Mar 25 '11 at 04:40
  • 1
    Based on "It seems that somehow Java is getting the idea that my main class is actually named the PATH" I think what happened was that %1 didn't have quotes around them and that the jar was located in a path including a space. – Fredrik Mar 25 '11 at 05:55
  • Thanks to both of you for the help! What a nightmare that was. I really hate it when the simple things go wrong... – aardvarkk Mar 25 '11 at 15:08
  • This is strange because i have the same problem, but it differs the way i generate jars. Like in Eclipse runable jar works fine, while using fat-jar plugin i see the same problem. Plus isn't it really strange that almost every other user has this problem, if i pakcage my application, the potential user won't know how to change file association? Do you think removing file assoc is the only reason for this problem? – Johnydep Jan 19 '12 at 23:01
  • From what i have seen, the problems occurs when you have additional referenced libraries in your build path that you have to include in the jar file you are exporting. Otherwise a single jar works fine, im sure there is more to it then just file associations. – Johnydep Jan 19 '12 at 23:03
  • Having messed up file associations is one way to get the error message. Usually, the error is caused by some other problem though. If you don't have the main class listed in the manifest in the jar file, or if the main class isn't in the jar file, I believe you will get this error message as well. – Jordan Miner Jan 20 '12 at 02:37
  • A wrong JRE version installed can often cause this error as well. For me the error showed up with JRE6 but when I installed JRE7 everything worked perfectly. – caw May 20 '12 at 13:41
11

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

tatactic
  • 1,379
  • 1
  • 9
  • 18
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\""
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
byby
  • 11
  • 1
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.

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.
Adam
  • 2,762
  • 1
  • 30
  • 32
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!)

tomato
  • 11
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.

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" %*
James
  • 1