Java反射中未找到类异常
您好,我正在使用此站点中的以下代码: http://java.sun.com /developer/technicalArticles/ALT/Reflection/
但是当我运行它时它显示异常 java.lang.ClassNotFoundException: A 可能我去错了地方请帮忙。 这是代码:
package com.Test;
class A {}
public class instance1 {
public static void main(String args[])
{
try {
Class cls = Class.forName("A");
System.out.println("gfsdga");
boolean b1
= cls.isInstance(new Integer(37));
System.out.println(b1);
boolean b2 = cls.isInstance(new A());
System.out.println(b2);
}
catch (Throwable e) {
System.err.println(e);
}
}
}
Hi I am using the following code from this site: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
But when I am running it it showing exception java.lang.ClassNotFoundException: A
May be I am going somewhere wrong Please help.
Here is the code:
package com.Test;
class A {}
public class instance1 {
public static void main(String args[])
{
try {
Class cls = Class.forName("A");
System.out.println("gfsdga");
boolean b1
= cls.isInstance(new Integer(37));
System.out.println(b1);
boolean b2 = cls.isInstance(new A());
System.out.println(b2);
}
catch (Throwable e) {
System.err.println(e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该类实际上称为
com.Test.A
因为您已在com.Test
包中声明了它 -Class.forName()
采用包限定的类名。 (请注意,com.Test
也是一个非常奇怪的包名称。)The class is actually called
com.Test.A
because you've declared it within thecom.Test
package -Class.forName()
takes the package-qualified class name. (Note thatcom.Test
is a pretty odd package name, too.)您需要使用
Class.forName("com.Test.A")
。You need
Class.forName("com.Test.A")
instead.