- public class ReflectMethod {
- //需求:写一个程序,能够根据用户提供的类名,去执行该类中的main方法。
- public static void main(String[] args) throws Exception
- {
-
- String startingClassName = args[0];
- Method mainMethod = Class.forName(startingClassName).getMethod("main",String[].class);
- //这里提示Method无法解析为类型。为什么呢????????????????
- //mainMethod.invoke(null,new Object[]{new String[]{"123","dfjdf","dkfjk"}});
- mainMethod.invoke(null,(Object)new String[]{"123","dfjdf","dkfjk"});
- }
- }
- class TestArguments
- {
- public static void main(String[] args) throws Exception
- {
- for (String arg : args)
- {
- System.out.println(arg);
- }
- }
- }
复制代码 |
|