class TestArgument中的内容如下:
class TestArgument
{
public static void main(String[] args) {
for(String str:args)
{
System.out.println(str);
}
}
}
主函数调用如下:
public class ReflectTest2 {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
String startClassName=args[0];
Method mainMethod=Class.forName(startClassName).getDeclaredMethod("main",String[].class);
mainMethod.invoke(null,(Object)new String[]{"111","222","333"});//这里显示参数的个数不对,main方法只接受一个字符串数组参数
//我们准备一个String[]给他按照道理说应该没什么问题,但是JDK1.5为了照顾JDK1.4,当给他一个字符串数组,人家要讲数组打开,打开的东西每一个都作为一个参数。
//那么我再在外面打一个包。
//mainMethod.invoke(null,new Object[]{new String[]{"111","222","333"}});
}