import java.lang.reflect.*;
class ClassRunner
{
public static void main(String[] args)
throws Exception
{
// Determine class and arguments for its main
Class clas = Class.forName(args[0]);
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, args.length - 1);
Object[] newArgVals = new Object[] {newArgs};
// Find its main and invoke it
Class[] argTypes = new Class[] {String[].class};
Method mainMethod = clas.getMethod("main", argTypes);
mainMethod.invoke(null, newArgVals);
}
}
class Echo
{
public static void main(String[] args)
{
// Echo args
System.out.println("Running Echo.main:");
for (int i = 0; i < args.length; ++i)
System.out.println("\targs[" + i + "] = " + args[i]);
}
}
/* Output to "java ClassRunner Echo one two three":
Running Echo.main:
args[0] = one
args[1] = two
args[2] = three
*/
End of Listing