2

I have literally tried everything to try and make my jar files executable by double clicking. But i have come to the conclusion that either I need some major help because my java installation has a problem, or I need to create a .cmd file to automatically run them properly. The code in the file would be like this:

java -jar myfile.jar

What would i replace myfile.jar with so that windows puts in the file extension of what i'm trying to open? Thanks.

Stefan Carlson
  • 372
  • 3
  • 7
  • 21
  • How are you building the jar? – S.R.I Sep 15 '13 at 11:47
  • possible duplicate of [how to run .jar file by double click on windows 7 (64)](http://stackoverflow.com/questions/8511063/how-to-run-jar-file-by-double-click-on-windows-7-64) – Lukas Knuth Sep 15 '13 at 11:47
  • Java won't run any jar files on my pc – Stefan Carlson Sep 15 '13 at 11:52
  • @LukasKnuth, more than that - if the OP built that jar themselves, they'd need some `Main-Class` attribute in their manifest file for java runtime to locate the class to execute. – S.R.I Sep 15 '13 at 11:53

3 Answers3

3

If you want to have a .cmd file to run your jar, then such a file could look like this

@ECHO OFF
SET JRE_HOME=<path to your jre>
%JRE_HOME%\bin\java.exe -jar myfile.jar

Note that here the option -jar that implicitly means that myfile.jar contains all your dependencies and you cannot extend the classpath to include other dependencies. Also using the this option require your mainifest to have the attibute Main-Class which tells which class to run / is the entry point for your program.

Better yet include myfile.jar in the classpath an pass your main class to java.exe

@ECHO OFF
SET JRE_HOME=<path to your jre>
SET MY_CLASSPATH=<jars/libs your app depends on separated by semicolon>;myfile.jar
%JRE_HOME%\bin\java.exe -cp %MY_CLASSPATH% <your main class>

Finally if you want to make an .exe of your java programm then you might want to use a wrapper like jsmooth which bundles your jar and all it's dependencies into a single .exe file

A4L
  • 17,353
  • 6
  • 49
  • 70
  • @a_horse_with_no_name, you're right, I should have named it something else, or at least expand it, but not completely overwriting it. Thanks for pointing that! – A4L Sep 15 '13 at 13:28
1

I Reinstalled Java, and now it works fine. Apparently, I had set WinRAR to be the default Jar opener for Minecraft modding. Re-installing Java reset the .jar extension, making the JRE the default instead of WinRAR.

Stefan Carlson
  • 372
  • 3
  • 7
  • 21
0

create a batch file open note pad type the following

set path="...<where u install java>/bin"

java -jar myfile.jar

save the notepad as filename.bat and put it in folder containing jar file double click it automatically run

just for example i give you a sample

@echo off
set path="F:/java/bin"
java -jar myfile.jar
pause

save it as filename.bat put it in a folder that contain filename.jar

if u can't success in above try this in batch file (it work if you install java runtime (jre)

set path =c:\Program Files\Java\jre7\bin\javaw.exe 
java -jar ImageEditor.jar
pause

ImageEditor is jar file note both jar and bat file must be in same folder

Hari Sri
  • 26
  • 4