Windows批处理文件多次运行JAR文件

5
我想制作一个批处理文件,从用户输入中运行jar X次。我已经查找了如何处理用户输入,但我不是完全确定。 在这个循环中,我想增加我发送到jar的参数。
目前为止,我不知道如何操作for循环中的变量numParam和strParam。
因此,当我从命令行运行这个小批处理文件时,我能够进行用户输入,但一旦它到达for循环,它就会输出“命令的语法不正确”。
到目前为止,我有以下内容:
@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

任何建议都会有所帮助。 编辑: 最近有些变化,似乎不能运行我的jar文件,或者至少不能运行我的测试回声程序。看起来我的用户输入变量没有被设置为我输入的内容,它保持在0。
2个回答

3

如果您阅读文档(在命令行中键入help forfor /?),则可以看到执行 FOR 循环固定次数的正确语法。

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

如果你想使用多行,那么你必须使用行继续符。

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

或者括号。
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
)

我已经阅读了for循环的帮助文档,正在尝试复制第一个提供的循环结构。 谢谢你的回复。这确实有助于解决for循环问题。 - Vnge

1
发生的问题是我的最后一个问题与变量扩展方式有关。这实际上是在dreamincode.net上得到的答案:Here 最终代码:
@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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接