写一个关于反射的联系,运行的时候报java.lang.ClassNotFoundException: Methods错误!求解
packageedu.sisv.struts2.helloWorld.reflect;importjava.lang.reflect.*;publicclassMetho...
package edu.sisv.struts2.helloWorld.reflect;
import java.lang.reflect.*;
public class Methods {
private int f1(Object p, int x) throws NullPointerException {
if (p == null)
throw new NullPointerException();
return x;
}
public static void main(String args[]) {
Class cls=null;
try {
cls = Class.forName("Methods");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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("-----");
}
}
} 展开
import java.lang.reflect.*;
public class Methods {
private int f1(Object p, int x) throws NullPointerException {
if (p == null)
throw new NullPointerException();
return x;
}
public static void main(String args[]) {
Class cls=null;
try {
cls = Class.forName("Methods");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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("-----");
}
}
} 展开
2个回答
展开全部
Class.forName的参数没有输入全,应该是包名+类名
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
cls = Class.forName("edu.sisv.struts2.helloWorld.reflect.Methods");
包名+类名,否则找不到,就异常了
java.lang.ClassNotFoundException: Methods
包名+类名,否则找不到,就异常了
java.lang.ClassNotFoundException: Methods
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询