- package learn.fanshe;
- import java.lang.reflect.Method;
- public class Test2 {
-
- public static void main(String[] args){
- for(String arg : args){
- System.out.println(arg);
- }
- }
-
- }
- class ReflectTest
- {
- public static void main(String[] args) throws Exception
-
- {
- Test2.main(new String[] {"123","abc","你好"});
- String className=args[0];
- Class clazz=Class.forName(className);
- // Class clszz = Class.forName(args[0]);
- // Method main = clszz.getMethod("main", String[].class);
- // main.invoke(null,"黑马程序员","黑马论坛","CSDN社区");
- Method methodMain=clazz.getMethod("main",String[].class);
- //方式一:强制转换为超类Object,不用拆包
- methodMain.invoke(null, (Object)new String[]{"123","456","789"});
- //方式二:将数组打包,编译器拆包后就是一个String[]类型的整体
- methodMain.invoke(null, new Object[]{new String[]{"123","456","789"}});
- }
- }
复制代码- 123
- abc
- 你好
- Exception in thread "main" java.lang.ClassNotFoundException: LearnJava.learn.fanshe.ReflectTest
- at java.net.URLClassLoader$1.run(Unknown Source)
- at java.net.URLClassLoader$1.run(Unknown Source)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
- at java.lang.ClassLoader.loadClass(Unknown Source)
- at java.lang.Class.forName0(Native Method)
- at java.lang.Class.forName(Unknown Source)
- at learn.fanshe.ReflectTest.main(Test2.java:22)
复制代码 我不知道为什么找不到类 java.lang.ClassNotFoundException: LearnJava.learn.fanshe.ReflectTest,想知道为什么?
求大神指点指点
|
|