简体   繁体   English

在Windows命令行上运行Java程序1000次

[英]Run Java program on Windows Command Line 1000 times

So I have a java program... it takes two arguments and I need to run it 1000 times. 所以我有一个java程序......它需要两个参数,我需要运行1000次。 The first argument doesn't change, but the second one needs go to from 1 to 1000. How do I do this? 第一个参数不会改变,但第二个参数需要从1到1000.我该怎么做? I've been trying to figure this out for a long while now :( 我一直试图想出这个问题很久了:(

Thanks in advance. 提前致谢。

Modify the program to take three arguments instead of two. 修改程序以取三个参数而不是两个。 Then use the second and third arguments to form a loop. 然后使用第二个和第三个参数来形成循环。

Originally your program maybe like: 最初你的程序可能像:

public static void main(String[] args) {
    String arg1 = args[0];
    String arg2 = args[1];

    //process using arg1 and arg2
}

Change it to the following: 将其更改为以下内容:

public static void main(String[] args) {
    String arg1 = args[0];
    String arg2 = args[1];
    String arg3 = args[2];

    int loopstart = Integer.parseInt(arg2);
    int loopend = Integer.parseInt(arg3);

    for (int i = loopstart; i <= loopend; i++) {
        //process using arg1 and i <-- take note
    }
}

Note: Repeatedly calling the program from a loop in a batch file is much slower, and less desirable than actually using a loop within the program itself. 注意:从批处理文件中的循环中反复调用程序要慢得多,并且比在程序本身中实际使用循环更不可取。

You can write a loop inside a .bat file and call the java program from it. 您可以在.bat文件中编写循环并从中调用java程序。 Something like: 就像是:

for /l %x in (1, 1, 1000) do (
 echo %x
 // call java using %x for the value of the current iteration
)

Calling java: How to run java application by .bat file 调用java: 如何通过.bat文件运行java应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM