- package again.reflect;
- import java.lang.reflect.Method;
- public class Test {
- public static void main(String[] args) throws Exception{
- String testClassName=args[0];//这个类要传入的参数是要测试的那个类名:again.reflect.TestArgument
- Method method=TestArgument.class.getMethod("main", String[].class);
- method.invoke(null, (Object)new String[]{"111","222","333"});
- //或者下面这种写法
- method.invoke(null, new Object[]{new String[]{"111","222","333"}});
- }
- }
- class TestArgument{
- public static void main(String []args){
- for(String str:args)
- System.out.println(str);
- }
- }
复制代码 invoke()方法就是执行你获得的那个方法,本例中就是:执行TestArgument.main(new Sting[]{"111","222","333"}); |