【问题标题】:Class Not Found Exception while using class Class使用类 Class 时未找到类异常
【发布时间】:2014-08-16 12:22:23
【问题描述】:

请检查以下代码.... 类 testError 已被实例化,但仍然生成 Class not found 异常...如果是这样,那么为什么写在异常处理程序中的语句没有被打印?

class testError
{
  void display()
    {
      System.out.println("This is testError Class");
    }

}

class checkResult
{
  public static void main(String[] args)
     {
        testError te = new testError();
        te.display();// I hope the class has been created

        Class cls = Class.forName("testError"); // will throw ClassNotFound exception
                                                // Why??... Though the class has been 
                                                // instantiated

        // if we try to put it in trycatch block it will work...Why??

         try{ Class cls = Class.forName("testError");}

         catch(ClassNotFoundException e)
            {
              System.out.println("Error found");  //"Error found" will not be printed
                                                  // as the class has been instantiated
            }

      }
   }

【问题讨论】:

  • 猜这是Java?您需要传递完全限定的类名。请参阅文档:docs.oracle.com/javase/7/docs/api/java/lang/…
  • 这些类真的在默认包中,还是在您定义的某个包中?
  • 嗨..这是在默认包中..问题是类已被实例化,ergo将在类路径中,但如果尝试执行代码,则会抛出未找到类的异常......
  • hii rparree....如果完整的类名是原因,那么 try catch 块中也会出现错误..但它的工作正常
  • 第一个Class.forName("testError");真的会抛出CNFException吗?还是 Java 只是抱怨您应该捕获此异常 - 因为在此之前您不会捕获或抛出任何异常?对于第二种情况,您可能应该阅读something about exception types in Java,然后查看Class.forName(...) method

标签: java classnotfound


【解决方案1】:

我无法发表评论——因为我的声誉太低了,但你的代码运行和调试都很好——尽管我不得不稍微修改一下才能让它编译:

    public static void main(String[] args) throws ClassNotFoundException {
    testError te = new testError();

    Class<?> cls = Class.forName("testError"); 
    try {
        cls = Class.forName("testError");
        // If you got there then everything went fine
        te.display();
    }
    catch (ClassNotFoundException e) {
        System.out.println("Error found"); 
    }
}

您如何运行代码(从命令行,在 IDE 中)?什么是控制台输出? 如果您希望人们调查您的问题,您必须提供更多有用的信息。

最后,Java 约定规定类名应以大写 字符(CheckResultTestError)开头。此外,您应该避免使用默认包中的类,因为这些类无法导入。

【讨论】:

  • hii...您的代码可以正常工作,因为您已经添加了 throws ClassNotFound 异常...我的问题是---> Class cls = Class.forName("testError");正在抛出 ClassNotFound 异常...如果 ClassNotFOund 事实为真,则 try catch 块中的打印语句必须在控制台上打印...
  • 您的代码可以正常工作,因为您已经添加了 throws ClassNotFound 异常...我的问题是 --->Class cls = Class.forName("testError");正在抛出 ClassNotFound 异常...如果 ClassNotFOund 事实为真,则 try catch 块中的打印语句必须在控制台上打印...
  • Do it as..first try to comment the try catch block--> ClassNotFound excption will be throw away....now comment //Class cls = Class.forName("testError") and取消注释 try catch 块..这次它会正常工作..为什么???..如果存在 ClassNotFound 异常,那么控件必须进入 try catch 块并且应该打印“发现错误”
  • 正如(我)预期的那样,注释掉 try/catch 块并没有改变任何东西,我仍然可以正常运行。我在程序结束时调用了te.display(),得到了This is testError Class,没有抛出异常...
【解决方案2】:

首先遵循java命名约定

公开你的主要课程

创建一些包(在默认包中创建不好),例如 mypackage 并将类放入其中

并尝试以这种方式调用该方法

String name = packageName.className.class.getName();//get the name of the class
className o = (className)Class.forName(name)
                               .newInstance();
//will give an instance of type Object so cast it
o.display();// call the method

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-13
    • 2018-08-25
    • 1970-01-01
    • 2013-04-09
    • 2016-09-13
    • 2016-10-01
    相关资源
    最近更新 更多