簡體   English   中英

Windows批處理文件多次運行jar文件

[英]Windows batch file to run jar file multiple times

我想制作一個從用戶輸入運行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

如果您閱讀文檔(從命令行輸入或for /?鍵入help for ),您將看到執行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
)

發生的事情是我的最后一個問題是變量如何擴展。 這實際上是在dreamincode.net回答的: 這里

最終代碼:

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM