2

Whenever I try to double-click a .jar file, it gives me a message saying that it can't locate or load the main class of the file. However, if I try to open the .jar file in the command line like this:

java -jar myfile.jar

Then, it runs. Is it something wrong the the system variables or something? I'm having a lot of trouble with java and uninstalling and reinstalling it again doesn't seem to solve it.

Thanks in advance!

EXTRA INFO: I'm running on Win7 64-bit, with both JDK 7 Update 9 (64-bit) and Java 7 Update 9 (64-bit). It also used to run .jar files just by double-clicking before, I don't know what happened.

MVR
  • 37
  • 4
  • 1
    What happens when you double-click the JAR on your particular system is up to your particular system. The best anyone can do is enumerate the ways you can look that up for yourself. – Marko Topolnik Nov 26 '12 at 20:53
  • 1
    Have you gone through the instructions from this [other thread](http://stackoverflow.com/questions/6084984/how-do-i-execute-jar-java-program-on-windows-7-command-line)? – eis Nov 26 '12 at 20:55
  • are there other jars that run as part of this program, or just a single jar? – jtahlborn Nov 26 '12 at 20:58
  • @MarkoTopolnik that's not a very useful answer, but ok. – MVR Nov 26 '12 at 20:59
  • @eis Thanks, lemme take a look – MVR Nov 26 '12 at 21:00
  • @jtahlborn the jar is the whole program, it doesn't need other files to run. The proof is that it runs with the command java -jar file.jar – MVR Nov 26 '12 at 21:00
  • @eis Eis, I read the thread you sent me and it is about running java on the command line, which I know how to do and specifically did, as told in the thread we are currently commenting. I want to run the jar file just by double-clicking it. It used to work before, I don't know what's happened. – MVR Nov 26 '12 at 21:03
  • @Richard the way it's explained there should make it work also when double clicking. The point is that if you configure the env so that you don't need to type `java -jar` prefix, it should work equally well by double clicking as well as just typing "jarfilename.jar". – eis Nov 26 '12 at 21:06
  • the reason i asked about other jars is that relative paths may work when run from the command line but not when double-clicking. so the fact that it works from the command line is not proof that no other jars are required. – jtahlborn Nov 26 '12 at 21:14

2 Answers2

3

Check that these two configurations are in place:

C:\>assoc .jar
    .jar=jarfile

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

With the latter pointing to the correct location of your javaw.exe. It should work then.

eis
  • 51,991
  • 13
  • 150
  • 199
  • I'd +2 this if I could. I've never seen the ftype command before. – David Nov 26 '12 at 21:11
  • Both are working, except you Program Files (x86) part is actually only Program Files and it's not jre6, it's jre7. – MVR Nov 26 '12 at 21:13
  • @Richard well that's environment specific. That's why I told "the latter pointing to the correct location...". So are you sure it is the correct place where it is pointing at? – eis Nov 26 '12 at 21:14
  • So if you take the output of that conf, say `"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1"`, put your jar file in the place of `%1`, and run the output manually, it works? – eis Nov 26 '12 at 21:18
  • Ok. There's also this [other thread](http://stackoverflow.com/questions/8511063/how-to-run-jar-file-by-double-click-on-windows-7-64) about a similar issue, check that out, too. – eis Nov 26 '12 at 21:28
  • Ok, I did the association to the javaw.exe and tried to open with it, but now, not only it pops up the black cmd screen, now it does nothing. Should it be javaw instead of only java? – MVR Nov 26 '12 at 21:36
  • I just tested with the java.exe instead of the javaw.exe you told me, just and it gave me the popup black cmd screen with the error. – MVR Nov 26 '12 at 21:38
  • @Richard maybe you missed the parameters at the end of the string, or any quotes along the way. – eis Nov 26 '12 at 21:42
  • Where could I have missed it? – MVR Nov 26 '12 at 21:46
  • @eis Help me please? I think we are almost solving the problem – MVR Nov 26 '12 at 23:14
  • @Richard by parameters I meant if you accidentally wiped out those `"%1" %*` in the process. They should've been added automatically, but one never knows. `ftype` will tell you. If else fails, you could try removing all java installations and then installing just one of them, and after that re-check the stuff we've been talking about. Don't have other ideas beyond that. – eis Nov 27 '12 at 06:49
-1

Double clicking on the jar in Win7 won't pass the -jar flag to the java program, so nothing will happen.

David
  • 1,887
  • 2
  • 13
  • 14