How to create a cmd file to run jars?

后端 未结 3 1795
梦毁少年i
梦毁少年i 2021-01-22 01:24

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 jav

3条回答
  •  长情又很酷
    2021-01-22 01:55

    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=
    %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=
    SET MY_CLASSPATH=;myfile.jar
    %JRE_HOME%\bin\java.exe -cp %MY_CLASSPATH% 
    

    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

提交回复
热议问题