Method1?这个类是你自己写的吗?java.lang.reflect 包中只有 Method 类(注意后边是没有数字“1”的哦),许是你写错了,把 Method1 换成 Method 试试?建议楼主把代码贴出来,呵呵。

解决方案 »

  1.   

    对,那个是我取的类名:
    public class Method1{public static void main(String args[]) {
            try {
                Class cls = Class.forName("method1");
                Method methlist[] = cls.getDeclaredMethods();
                for (int i = 0; i < methlist.length; i++) {
                    Method m = methlist[i];
                    System.out.println("name = " + m.getName());
                    System.out.println("decl class = " + m.getDeclaringClass());
                    Class pvec[] = m.getParameterTypes();
                    for (int j = 0; j < pvec.length; j++)
                        System.out.println("param #" + j + " " + pvec[j]);
                    Class evec[] = m.getExceptionTypes();
                    for (int j = 0; j < evec.length; j++)
                        System.out.println("exc #" + j + " " + evec[j]);
                    System.out.println("return type = " + m.getReturnType());
                    System.out.println("-----");
                }
            } catch (Throwable e) {
                System.err.println(e);
            }
        }
    }
      

  2.   

    哦,那边有个小错误, Class cls = Class.forName("method1"); 
    将method1改为Method1后运行还是不行:java.lang.ClassNotFoundException: Method1 
      

  3.   

    嗯,你已经把一些地方的Method1改成Method了吧?
    再把Class.forName("method1");中的method1首字母改成大写就成了。
    public class Method1{ public static void main(String args[]) {
            try {
                Class cls = Class.forName("Method1");
                Method methlist[] = cls.getDeclaredMethods();
                for (int i = 0; i < methlist.length; i++) {
                    Method m = methlist[i];
                    System.out.println("name = " + m.getName());
                    System.out.println("decl class = " + m.getDeclaringClass());
                    Class pvec[] = m.getParameterTypes();
                    for (int j = 0; j < pvec.length; j++)
                        System.out.println("param #" + j + " " + pvec[j]);
                    Class evec[] = m.getExceptionTypes();
                    for (int j = 0; j < evec.length; j++)
                        System.out.println("exc #" + j + " " + evec[j]);
                    System.out.println("return type = " + m.getReturnType());
                    System.out.println("-----");
                }
            } catch (Throwable e) {
                System.err.println(e);
            }
        }
    }
      

  4.   

    你把整个源代码贴上来,包括 import 及 package ,呵呵。
      

  5.   

    你得写上Method1的完整名称,比如你的Method1类是放在test包中,
    则你应该写:Class.forName( "test.Method1" );
      

  6.   

    这是我改后的,工程上的类名我确定也是Method1,按说这是没错啊,怎么运行后不行呢?package com.mms.reflection;
    import java.lang.reflect.*;public class Method1 {
       
        public static void main(String args[]) {
            try {
                Class cls = Class.forName("Method1");
                Method methlist[] = cls.getDeclaredMethods();
                for (int i = 0; i < methlist.length; i++) {
                    Method m = methlist[i];
                    System.out.println("name = " + m.getName());
                    System.out.println("decl class = " + m.getDeclaringClass());
                    Class pvec[] = m.getParameterTypes();
                    for (int j = 0; j < pvec.length; j++)
                        System.out.println("param #" + j + " " + pvec[j]);
                    Class evec[] = m.getExceptionTypes();
                    for (int j = 0; j < evec.length; j++)
                        System.out.println("exc #" + j + " " + evec[j]);
                    System.out.println("return type = " + m.getReturnType());
                    System.out.println("-----");
                }
            } catch (Throwable e) {
                System.err.println(e);
            }
        }
    }
      

  7.   

    要不你就把
    Class cls = Class.forName( "Method1" );
    改成:
    Class cls = Method1.class;
      

  8.   

    哦,可以了,就是那个Class.foName("Method1")出问题了,为什么要用类全名呢?
      

  9.   

    行,我终于知道了。呵呵。两种正确写法。第一种:
    Class cls = Method1.class;第二种:
    Class cls = Class.forName("com.mms.reflection.Method1"); 
      

  10.   

    嗯,用Class cls = Method1.class;这个也可以跑起来的,为什么呢?
      

  11.   


    因为 Method1.class 所得到的也是与 Method1 相关的 Class 对象,呵呵。
    这样吧,给你段代码,你一定能明白的,呵呵:package com.mms.reflection;import java.lang.reflect.Method;
    public class Method1{ public static void main(String args[]) throws ClassNotFoundException {
            Class c1 = Method1.class;
            Class c2 = Class.forName("com.mms.reflection.Method1");
            
            if( c1 == c2 )
             System.out.println( "这两玩艺原来是同一个东西!!" );
        }
    }
      

  12.   

    而且它两个的内存都一样的用==测试为true
      

  13.   

    刚看一下API,才找到,就我们这个理,一样的。
      

  14.   

    是的, Class c1 = Method1.class;
    Class c2 = Class.forName("com.mms.reflection.Method1");内存中一样.