Java反射中未找到类异常

发布于 2024-12-25 12:28:34 字数 829 浏览 2 评论 0原文

您好,我正在使用此站点中的以下代码: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2025-01-01 12:28:34

该类实际上称为 com.Test.A 因为您已在 com.Test 包中声明了它 - Class.forName() 采用包限定的类名。 (请注意,com.Test 也是一个非常奇怪的包名称。)

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.)

愛放△進行李 2025-01-01 12:28:34

您需要使用Class.forName("com.Test.A")

You need Class.forName("com.Test.A") instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文