0

So I have a java program... it takes two arguments and I need to run it 1000 times. The first argument doesn't change, but the second one needs go to from 1 to 1000. How do I do this? I've been trying to figure this out for a long while now :(

Thanks in advance.

lrogar
  • 59
  • 1
  • 8
  • Read [the tutorial](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). – MarsAtomic Feb 23 '15 at 01:43
  • 2
    Why do you need to do this? Launching the JVM 1000 times is going to take much longer then writing a loop from 1 to 1000 in Java. – Elliott Frisch Feb 23 '15 at 01:44
  • How come you can't use a `for` loop? Why do you need to re-execute the program 1000 times with a slightly different input each time? – rayryeng Feb 23 '15 at 01:59
  • I'm supposed to plot the time it takes the program to run using different buffer sizes. – lrogar Feb 23 '15 at 02:14

2 Answers2

0

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.

ADTC
  • 8,999
  • 5
  • 68
  • 93
  • 1
    I thought it could just be done directly on the command line but I'll just do it this way... so simple haha. Thank you. – lrogar Feb 23 '15 at 02:24
0

You can write a loop inside a .bat file and call the java program from it. 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

Community
  • 1
  • 1
dambros
  • 4,252
  • 1
  • 23
  • 39