Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open runnable .jar file [duplicate]

Tags:

java

eclipse

jar

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

like image 233
g4m3r2 Avatar asked Apr 24 '26 08:04

g4m3r2


1 Answers

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!

like image 132
paulsm4 Avatar answered Apr 26 '26 00:04

paulsm4