[英]Class not found exception in Java Reflection
Hi I am using the following code from this site:http://java.sun.com/developer/technicalArticles/ALT/Reflection/嗨,我正在使用来自该站点的以下代码: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.但是当我运行它时,它显示异常java.lang.ClassNotFoundException: A可能是我去错了地方请帮忙。 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); } } }
The class is actually called com.Test.A
because you've declared it within the com.Test
package - Class.forName()
takes the package-qualified class name. The class is actually called com.Test.A
because you've declared it within the com.Test
package - Class.forName()
takes the package-qualified class name. (Note that com.Test
is a pretty odd package name, too.) (请注意, com.Test
也是一个非常奇怪的 package 名称。)
You need Class.forName("com.Test.A")
instead.您需要Class.forName("com.Test.A")
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.