Search code examples
javaperformanceprocessjvmprogram-entry-point

How many JVM calling main inside main


class B {
    public static void main(String[] args) {
        
    }
}
class A {
    public static void main(String[] args) {
        B.main(args);
    }
}

In the above flow, my init method is A.main which in turn calls B.main.

  1. I know calling A.main will spawn a JVM. Does calling B.main inside A.main spawn another JVM? OR
  2. B.main is JUST another static method once a JVM is started on A.main as init function.

Solution

  • Option 2. The mains are just static methods of each class, and only one JVM is running when making the call from A to B.main(args).

    You can also make use of this in JUNIT tests to help check the command line launch behaves as expected, such as

    @Test void coverage() {
       A.main(new String[] { "a","b" }); // or B.main
       // assertions here if there is some output state you could check
    }