import java.lang.reflect.*; class Runner { static void runMain(Class cls, String[] mainArg) { Class firstArgType = String[].class; Class secondArgType = Integer.class; try { Method mainMethod = cls.getDeclaredMethod("main", firstArgType); // behaves as main(mainArg) mainMethod.invoke(null, new Object[] {mainArg}); // below, they are the same mainMethod.invoke(null, arg1, arg2, arg3); mainMethod.invoke(null, new Object[] {arg1, arg2, arg3}); // below, they are the same mainMethod.invoke(null, mainArg); mainMethod.invoke(null, mainArg[0], mainArg[1], ...); } catch (NoSuchMethodException e) { System.err.println("main cannot be found!"); } catch (IllegalAccessException e) { System.err.println("main cannot be accessed!"); } catch (InvocationTargetException e) { System.err.println("I don't even know what just happened!"); } } public static void main(String[] args) { System.out.println("I'm the one-argument main"); for (String s : args) System.out.println(s); } public static void main(String[] args, Integer n) { Class stringCls = String.class; System.out.println("I'm the two-argument main"); } }