2

Suppose I have a Java program Test.class, if I used the below script

for i in 1..10
do
    java Test
done

the JVM would exit each time java Test is invoked.

What I want is running java Test multiple times without exiting the JVM, so that, the optimized methods in prior runs can be used by later runs, and possibly be further optimized.

JackWM
  • 10,085
  • 22
  • 65
  • 92
  • Your lines "The JVM is supposed to exit every time java Test is invoked." and "I want to run java Test multiple times without exiting the JVM" contradict each other! – adarshr Aug 16 '12 at 17:11
  • 2
    Why don't you just write a wrapper and run `main()` multiple times? – Jochen Aug 16 '12 at 17:12

7 Answers7

4

Why not write a Java program that repeatedly calls the entry point of the program you want to run?

Nicolas Mommaerts
  • 3,207
  • 4
  • 35
  • 55
3

Run this once

java Run

The main Program calling your Test class mulitple times.

class Run {

    public static void main(String[] args) {
        for(int i=0; i<10; i++){
            Test.main(args);
        }
    }
}
mtk
  • 13,221
  • 16
  • 72
  • 112
  • That's the solution that comes to mind. There is one caveat though: static initializers will be executed only once, so if there is anything in the program that relies on static initializers, this is very likely *not* giving the same result as running the program 10 times in a shell script. – Jochen Aug 16 '12 at 17:25
  • @Joc I believe you are referencing `static block` here. Why not move the code of `static block` into a `static method`, and call the `static method` each time in the for loop before calling the `main`? What's your thought over this? – mtk Aug 16 '12 at 17:36
  • @mtk That would work for static blocks. But if you initialize static fields of the class like `static int someValue = 42;`, you'd also have to move all those statements into an explicit block. – Jochen Aug 16 '12 at 17:39
2
public class RunTest
 {
    public static void main(String[] args)
      {
          for(int i=1; i<=10; i++)
              Test.main(args); 
      }
 }

This should do it. You can call the Test class' main function with the same arguments you pass to your main function. This is the "wrapper" that another poster referred to.

CodeBlue
  • 14,631
  • 33
  • 94
  • 132
1

Use code like this:

public static void main2(String args[]){ // change main to main2
  for(int i=0;i<10;i++)main(args);
}
public static void main(String args[]){ // REAL main
   //...
}
DankMemes
  • 2,085
  • 2
  • 21
  • 30
0

Perhas a ProcessBuilder would be suitable? E.g., something like

String[] cmd = {"path/to/java", "test"};
ProcessBuilder proc = new ProcessBuilder(cmd);
Process process = proc.start();
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
  • 1
    But that does create a new process every time, which is exactly what he doesn't want – Jochen Aug 16 '12 at 17:13
  • It says "*without exiting the JVM*", which it *technically* doesn't. Depends on how the phrasing is interpreted. – Johan Sjöberg Aug 16 '12 at 17:15
  • That is true. But he was talking about optimizations in Test. The JIT optimizations would be lost or at least not be reused between different instances created with `proc.start()`. – Jochen Aug 16 '12 at 17:23
  • We should pull all this up into the question to make clear what's being asked ;) – Jochen Aug 16 '12 at 17:26
0

What you want to do is put the loop inside the Test class or pass the number of times you want to loop as an argument to the main() inside Test.

NOTE:

If you are looking at JIT optimizations it will take more than 10 iterations to kick in.

Ajay George
  • 11,759
  • 1
  • 40
  • 48
  • Sorry, I didn't get your point of your NOTE. could you please explain a little more? "kick in" ? – JackWM Aug 16 '12 at 17:21
  • Refer http://en.wikipedia.org/wiki/Just-in-time_compilation. The JVM will identify hotspots within your code and try to optimize it. – Ajay George Aug 16 '12 at 17:57
0

Same as previous examples but using command line args

public class Test {

    public static void main(String[] args) {
        if ( args.length > 0) {
            final Integer n = Integer.valueOf(args[0]);
            System.out.println("Running test " + n);
            if ( n > 1 ) {
                main(new String[] { Integer.toString(n-1) });
            }
        }
    }
}

use the args to indicate the number of runs

$ java Test 10
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134