java bat file to run jar

How to run jar file from batch file?

Overview

In this post, we will learn to run a .jar file from a .bat file.

Let’s say, we have a JAR file and want to run it using a batch file. So that, we can double click on it and start our Java application.

Note

The batch and bat words are used to refer same thing. The only difference is that, batch is the file format that holds some command to do some action and the bat is the extension of the batch file.

Solution

First, we need to create a file with extension .bat.

For example, I have a HelloWorld.jar file and want to run it from batch script.

In this case, Let’s create a HelloWorld.bat file. (I would prefer to keep the same name as my jar file for bat.)

Note:

If you are creating the bat file using notepad then make sure to select Save as type: All. If you don’t do this, even you give the file name HelloWorld.bat it still saves the file as a text document.

Let’s add content to our batch file:

@ECHO OFF
start java -jar HelloWorld.jar

With this code, make sure your .jar and .bat files are in the same location.

If you want to keep your jar file and bat file in different locations then you need to provide the relative location of your jar file.

Now, we can run the batch file by double-clicking on HelloWorld.bat file.

When we double click on the bat file, then it should automatically start a jar file.

Conclusion

Hence, we are able to run our JAR file with the help of a batch (bat) file.


Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments