Method1?这个类是你自己写的吗?java.lang.reflect 包中只有 Method 类(注意后边是没有数字“1”的哦),许是你写错了,把 Method1 换成 Method 试试?建议楼主把代码贴出来,呵呵。
解决方案 »
- 悲催一天都没弄明白的找不到org/apache/commons/logging的问题,迫切求助!
- 谁帮我看下我代码哪错了撒
- acle创建索引 怎么创建聚集索引,非聚集索引,什么时候又该使用??
- 在线等:Windows 区域设置为中文(中国)时是对的,为英语(美国)时是乱码
- 急~ 我刚从软件培训学校毕业,但是还没找到工作,请问各位像我这种菜鸟如何才能迈入IT行业,如何才能在IT业立足,学校期间学习了Java以及C#编程语言Html JavaScript以及sql数据库
- 以前还好好的项目,现在怎么运行不起来了
- 求用jb开发hibernate的基础例子。谢谢大家。
- 寻高手一起完善一个验证小插件
- tomcat中的Context path 起和作用 那位能详细的说下 谢谢
- 怎么用Java编写这个页面,求大神告知
- tomcat6 servlet 的错误,在线等,谢谢了
- CVS checkout项目文件出现乱码!
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);
}
}
}
将method1改为Method1后运行还是不行:java.lang.ClassNotFoundException: Method1
再把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);
}
}
}
则你应该写:Class.forName( "test.Method1" );
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);
}
}
}
Class cls = Class.forName( "Method1" );
改成:
Class cls = Method1.class;
Class cls = Method1.class;第二种:
Class cls = Class.forName("com.mms.reflection.Method1");
因为 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( "这两玩艺原来是同一个东西!!" );
}
}
Class c2 = Class.forName("com.mms.reflection.Method1");内存中一样.