Run .jar from batch-file

I have created an executable .jar file. How can I execute the .jar using a batch-file without mentioning a class path?


On Windows you can use the following command.

start javaw -jar JarFile.jar

By doing so, the Command Prompt Window doesn't stay open.


There is a solution to this that does not require to specify the path of the jar file inside the .bat. This means the jar can be moved around in the filesystem with no changes, as long as the .bat file is always located in the same directory as the jar. The .bat code is:

java -jar %~dp0myjarfile.jar %*

Basically %0 would expand to the .bat full path, and %~dp0 expands to the .bat full path except the filename. So %~dp0myjarfile.jar is the full path of the myjarfile.jar colocated with the .bat file. %* will take all the arguments given to the .bat and pass it to the Java program. (see: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true )


You can create a batch file with .bat extension with the following contents

Use java for .jar that does not have UI and is a command line application

@ECHO OFF
start java -jar <your_jar_file_name>.jar

Use javaw for .jar that has a UI

@ECHO OFF
start javaw -jar <your_jar_file_name>.jar

Please make sure your JAVA_HOME is set in the environment variables.


If you want a batch file to run a jar file, make a blank file called runjava.bat with the contents:

java -jar "C:\myjarfile.jar"

cd "Your File Location without inverted commas"

example : cd C:\Users*****\Desktop\directory\target

java -jar myjar.jar

example bat file looks like this:

@echo OFF
cd C:\Users\****\Desktop\directory\target
java -jar myjar.jar

This will work fine.