Home > Java Core > Windows BAT file to execute runnable jar file and checks if it is already running

Windows BAT file to execute runnable jar file and checks if it is already running

Java

Windows BAT file to execute runnable jar file and checks if it is already running

Suppose you have a Java Desktop application, for instance, a SWING application. How would you like to present the runnable JAR file to the client? Am pretty sure many would prefer the BAT script file in case the target machine is WINDOWS based and the SHELL script file if the target machine is LINUX based to invoke the runnable JAR. I have searched over the INTERNET and you can easily get the SHELL script to invoke the runnable JAR file. SO, in this post, I would provide the script in order to write a BAT file that checks if there is already the concerned JAR running, if not it will invoke the runnable JAR file. The advantage of this is you do not want to expose the JAR file directly to the user, and you do not want more than 1 process of JAR running at a particular instance.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1" %%i in ('jps -l ^| find "yourjar.jar"') do (
   goto end
)
start javaw -jar yourjar.jar
:end