3

I have created a runnable .jar file on Eclipse (right click on project --> export --> extract required libraries), and saved it to my desktop. However, the file doesn't run when I double click it, and it does not even open when I use the command prompt of java -jar jarfilename.jar

message that shows up reads:

unable to access jarfile jarfilename.jar.

I tried: 1. Downloading and running Jarfix.exe to no avail. 2. Made sure I have JDK and JRE (I have Java 7 update 79, 8 update 5, and 8 update 71; Java SE Dev Kit 7 update 79 and 8 update 5)

Any help would be greatly appreciated. I am using a Windows PC.

**Edit: After trying out multiple steps, I think the problem was not specifying the .jar file properly. I needed to type java -jar C:\Path to File\myJar.jar.

Now if we can figure out why the double-click doesn't get the file to run...

g4m3r2
  • 55
  • 5

2 Answers2

4

Several different things could be going wrong here:

  1. It's your OS/Desktop that "runs" a shortcut.

    So on Windows, you need to register file type "jar" with Java:

    How to run .jar file by double click on Windows 7 (64)

  2. You also need to have a "runnable .jar file"

    That means you must specify the class with the "main()" you want to run in MANIFEST.MF before you export the .jar file:

    How to make an executable jar file?

  3. Finally, you need to actually "export" from Eclipse AS a .jar file. Double-check the "export" you ran from Eclipse and make sure you explicitly specified "export as .jar"

ADDENDUM: You seem to have made a couple of additional boo-boos:

  1. You do NOT want "Eclipse > project> export> extract required libraries". This copies extraneous junk into your .jar file. Two disadvantages: a) your .jar will probably be bigger than it needs to be (probably a LOT bigger!), and b) you might make your .jar INCOMPATIBLE with your Java runtime. Don't do it :)

  2. for the error java -jar jarfilename.jar, you must do this:

    a) cd \my\jar\directory

    b) %JAVA_HOME%\java -jar myjarfile.jar

    ... OR ...

    a) %JAVA_HOME%\java -jar \my\jar\directory\myjarfile.jar

PS: You still haven't told us your OS (Windows? Linux? MacOS? Other?). I'm guessing "Windows"...

PPS:

If you still can't get it working, please follow this tutorial:

http://www.mkyong.com/java/how-to-make-an-executable-jar-file/

Follow the instructions verbatim, using the command prompt. Post back any questions you have about the tutorial. Once you get the tutorial working (and you should, without too much difficulty), you can apply it back to your original Eclipse project.

Please let us know what you find!

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks for your reply. The first method (Start "Control Panel" Click "Default Programs" Click "Associate a file type or protocol with a specific program" Double click .jar Browse C:\Program Files\Java\jre7\bin\javaw.exe Click the button Open Click the button OK) seemed to yield no result. 2. For this possible solution, I pasted the code javax.swing... where it told me to, but received two error messages: Access restriction: the method InvokeLater(runnable) from the type Swing Utilities is not accessible due to restriction on required library C:\Program Files(x86)/java/jre1.8.0_71/lib/rt.jar – g4m3r2 Feb 21 '16 at 01:50
  • ..as well as Access restriction: The type swingUtilities is not accessible due to restriction on required library C:\Program Files(x86)/java/jre1.8.0_71/lib/rt.jar. 3. I tried saving the file as .jar and runnable .jar, but neither method seems to work. – g4m3r2 Feb 21 '16 at 01:55
0

I assume in the export wizard you select a WORKING run configuration? That will make sure, that the correct main-class is put into the manifest file.

An jar file is nothing but a "zip". So you should be able to open it with e.g. 7zip and see what´s in it (class-files, manifest, etc).

Please post the error when executing java -jar jarfile.jar

ty

Rainer
  • 761
  • 5
  • 20