Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch file to run jar file multiple times

Id like to make a batch file that runs a jar X amount of times from user input. I have looked for how to handle user input, but I am not totally sure. In this loop I'd like to increase the parameters I am sending to the jar.

As of now, I do not know

  • manipulate the variables in the for loop, numParam, strParam

So, when i run this little bat file from the command line, I get am able to do user input, but once it gets to the for loop, it spits out "The syntax of the command is incorrect

So far I have the following

@echo off

echo Welcome, this will run Lab1.jar
echo Please enter how many times to run the program
:: Set the amount of times to run from user input
set /P numToRun = prompt


set numParam = 10000
set strParam = 10000
:: Start looping here while increasing the jar pars
:: Loop from 0 to numToRun
for /L %%i in (1 1 %numToRun%) do (
    java -jar Lab1.jar %numParam% %strParam%

)
pause
@echo on

Any suggestion would be helpful

Edit: With the recent change, it doesnt seem to run my jar file. or at least doesnt seem to run my test echo program. It seems that my user input variable is not being set to what I have inputted, it stays at 0

like image 732
Vnge Avatar asked Oct 06 '22 17:10

Vnge


2 Answers

If you read the documentation (type help for or for /? from the command line) then you will see the correct syntax for executing a FOR loop a fixed number of times.

for /L %%i in (1 1 %numToRun%) do java -jar Lab1.jar %numParam% %strParam%

If you want to use multiple lines, then you must either use line continuation

for /L %%i in (1 1 %numToRun%) do ^
  java -jar Lab1.jar %numParam% %strParam%

or parentheses

for /L %%i in (1 1 %numToRun%) do (
  java -jar Lab1.jar %numParam% %strParam%
  REM parentheses are more convenient for multiple commands within the loop
)
like image 193
dbenham Avatar answered Oct 10 '22 02:10

dbenham


What happened was for my last issues was something with how the variables expanded. This was actually answer at dreamincode.net: Here

Final code:

@echo off

echo Welcome, this will run Lab1.jar
:: Set the amount of times to run from user input
set /P numToRun= Please enter how many times to run the program: 

set /a numParam = 1000
set /a strParam = 1000

setlocal enabledelayedexpansion enableextensions


:: Start looping here while increasing the jar pars
:: Loop from 0 to numToRun
for /L %%i in (1 1 %numToRun%) do (
    set /a numParam = !numParam! * 2
    set /a strParam = !strParam! * 2
    java -jar Lab1.jar !numParam! !strParam!

    :: The two lines below are used for testing
    echo %numParam%  !numParam!
    echo %strParam%  !strParam!
)

@echo on
like image 25
Vnge Avatar answered Oct 10 '22 02:10

Vnge