A reason might be that if you use reflection, you do not need to have a compile-time dependency on the class that you are invoking the main
method of. You can load the class dynamically, using Class.forName(...)
, then use reflection to find the main
method and invoke it.
String name = "com.mycompany.MyMainClass";
// Load the class dynamically
Class<?> mainClass = Class.forName(name);
// Find the 'main' method that takes a string array
Method mainMethod = mainClass.getMethod("main", new Class<?>[] { String[].class });
// Invoke the method; first arg is null, because the method is static
mainMethod.invoke(null, new String[] { "one", "two" });