有这样一段测试代码:
package org.com.Test;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/**
* @author Administrator
*
*/
public class GTest {
public static void show(String... s) {
System.out.println(s.length);
} /**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> gtClass=Class.forName("org.com.Test.GTest");
Method method=gtClass.getMethod("show", String[].class);
//method.invoke(gtClass.newInstance(),"1","2");
//method.invoke(gtClass.newInstance(),new String[]{"1","2"});
}
}
最后执行这个方法时候这里要怎么给参数这个方法才能正常执行?
注释的这两行一个是报:
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
还有一个报:
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
请指点一下,谢谢!
package org.com.Test;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/**
* @author Administrator
*
*/
public class GTest {
public static void show(String... s) {
System.out.println(s.length);
} /**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> gtClass=Class.forName("org.com.Test.GTest");
Method method=gtClass.getMethod("show", String[].class);
//method.invoke(gtClass.newInstance(),"1","2");
//method.invoke(gtClass.newInstance(),new String[]{"1","2"});
}
}
最后执行这个方法时候这里要怎么给参数这个方法才能正常执行?
注释的这两行一个是报:
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
还有一个报:
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
请指点一下,谢谢!
参数数量错误
参数类型不匹配!
问题是,invoke方法第二个形参是Object...args
传两个String类型的应该可以的呀,就是这里不解,
请教一下