How to create a cmd file to run jars?

21,689

Solution 1

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

Solution 2

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.

Share:
21,689
Stefan Carlson
Author by

Stefan Carlson

I started programming in Java when I was 11, and became proficient at 13. I've been on here ever since.

Updated on May 11, 2020

Comments

  • Stefan Carlson
    Stefan Carlson almost 4 years

    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.