Java Class.forName 方法 java.lang.ClassNotFoundException;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22433096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:41:03  来源:igfitidea点击:

Java Class.forName method java.lang.ClassNotFoundException;

javaclassclassnotfoundexceptionclassnotfound

提问by webish

I've found other thread where people had and solved this error; however, all of were NOT using fully qualified class paths. I cannot seem to get Class.forName to work and I am using fully qualified paths.

我找到了人们解决此错误的其他线程;然而,所有这些都没有使用完全限定的类路径。我似乎无法让 Class.forName 工作,我正在使用完全限定的路径。

I've tested compiling from CLI and using ItelliJ Idea. Both fail with the same error.

我已经测试过从 CLI 编译并使用 ItelliJ Idea。两者都失败并出现相同的错误。

Code (in test directory):

代码(在测试目录中):

package test;
public class Test {
  public static void main(String[] args) {
    Class cls = Class.forName("java.util.ArrayList");
  }
}

Error:

错误:

java.lang.ClassNotFoundException; must be caught or declared to be thrown

java.lang.ClassNotFoundException; 必须被捕获或声明被抛出

The example above does NOT work. Thanks in advance!

上面的例子不起作用。提前致谢!

采纳答案by Mauren

You're getting this message because ClassNotFoundExceptionis a checked exception. This means that this exception can not be ignored. You need to either surround it with a try/catchconstruct and provide exception handling or add a throwsclause to your method and handle it in a callee.

您收到此消息是因为ClassNotFoundException是已检查异常。这意味着这个异常不能被忽略。您需要用一个try/catch构造包围它并提供异常处理,或者throws向您的方法添加一个子句并在被调用方中处理它。

EDIT:Please note that Class.forName()construct is not resolved during compilation. When you write such a statement, you're telling the JVM to look during program executionfor a class which may not have been loaded. Java libraries are dynamically linked instead of statically linked, meaning that their code is not incorporated in your program code, being loaded only when requested. This is why it throws ClassNotFoundException.

编辑:请注意,Class.forName()在编译期间未解析构造。当您编写这样的语句时,您是在告诉 JVM在程序执行期间查找可能尚未加载的类。Java 库是动态链接而不是静态链接的,这意味着它们的代码不会合并到您的程序代码中,仅在请求时加载。这就是它抛出ClassNotFoundException.

回答by Abimaran Kugathasan

Class.forName("java.util.ArrayList")method declared to throw a checked exception ClassNotFoundException, so you must handle it inside a try/catchblock like following

Class.forName("java.util.ArrayList")声明为抛出已检查异常的方法ClassNotFoundException,因此您必须在try/catch如下块中处理它

package test;
public class Test {
  public static void main(String[] args) {
    try {
        Class cls = Class.forName("java.util.ArrayList");
    } catch (ClassNotFoundException e) {
    // Handle it here
    }
  }
}

回答by Willem Van Onsem

There are two options:

有两种选择:

  1. Surround with a try-catchblock and do some appropriate handling:

    package test;
    public class Test {
      public static void main(String[] args) {
        try {
            Class cls = Class.forName("java.util.ArrayList");
        } catch(ClassNotFoundException e)  {
            System.out.println("This is not a very serious exception, or at least Test knows how to handle it");
        }
      }
    }
    
  2. Throw it outside the method in the hope that some calling method will know what to do.

    package test;
    public class Test {
      public static void main(String[] args) throws ClassNotFoundException {
        Class cls = Class.forName("java.util.ArrayList");
      }
    }
    
  1. try-catch块包围并做一些适当的处理:

    package test;
    public class Test {
      public static void main(String[] args) {
        try {
            Class cls = Class.forName("java.util.ArrayList");
        } catch(ClassNotFoundException e)  {
            System.out.println("This is not a very serious exception, or at least Test knows how to handle it");
        }
      }
    }
    
  2. 将它扔到方法之外,希望某些调用方法知道该做什么。

    package test;
    public class Test {
      public static void main(String[] args) throws ClassNotFoundException {
        Class cls = Class.forName("java.util.ArrayList");
      }
    }
    

回答by Hosein Masbough

Try this:

尝试这个:

try{
  Class cls = Class.forName("java.util.ArrayList");
}catch(ClassNotFoundException e){
  ... do messaging or logging
}

or throw ClassNotFoundExceptionin the methods signature:

throw ClassNotFoundException在方法签名中:

public static void main(String[] args) throws ClassNotFoundException {

回答by Reed Grey

I don't know why none of the answers above can explain why this error happened. Neither can I, but I once encountered this issue and solved it by using

我不知道为什么上面的答案都不能解释为什么会发生这个错误。我也不能,但是我曾经遇到过这个问题并通过使用解决了它

Thread.currentThread().getContextClassLoader().loadClass($YOUR_CLASS_NAME)

Thread.currentThread().getContextClassLoader().loadClass($YOUR_CLASS_NAME)

Hope it can solve your problem and hope someone can give us an explanation.

希望它能解决您的问题,希望有人能给我们一个解释。