Java ClassNotFoundException (Java ClassNotFoundException)
- Java ClassNotFoundException occurs when the application tries to load a class but Classloader is not able to find it in the classpath. 当应用程序尝试加载类,但Classloader无法在类路径中找到它时,将发生Java ClassNotFoundException。
- Common causes of
java.lang.ClassNotFoundException
are usingClass.forName
orClassLoader.loadClass
to load a class by passing String name of a class and it’s not found on the classpath.java.lang.ClassNotFoundException
常见原因是使用Class.forName
或ClassLoader.loadClass
通过传递类的String名称来加载类,而在类路径上找不到它。 - ClassNotFoundException is a checked exception, so it has to be catch or thrown to the caller. ClassNotFoundException是一个已检查的异常 ,因此必须将其捕获或抛出给调用者。
- ClassNotFoundException always occurs at runtime because we are indirectly loading the class using Classloader. Java compiler has no way to know if the class will be present in the classpath at runtime or not. ClassNotFoundException总是在运行时发生,因为我们使用Classloader间接加载类。 Java编译器无法知道在运行时该类是否存在于类路径中。
- One of the most common example of ClassNotFoundException is when we try to load JDBC drivers using
Class.forName
but forget to add it’s jar file in the classpath. ClassNotFoundException的最常见示例之一是,当我们尝试使用Class.forName
加载JDBC驱动程序但忘记将其jar文件添加到类路径中时。
Java ClassNotFoundException示例 (Java ClassNotFoundException Example)
Let’s look at a simple example where we will get ClassNotFoundException
.
让我们看一个简单的示例,在该示例中将获得ClassNotFoundException
。
package com.journaldev.exceptions;
public class DataTest {
public static void main(String[] args) {
try {
Class.forName("com.journaldev.MyInvisibleClass");
ClassLoader.getSystemClassLoader().loadClass("com.journaldev.MyInvisibleClass");
ClassLoader.getPlatformClassLoader().loadClass("com.journaldev.MyInvisibleClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Note that com.journaldev.MyInvisibleClass
doesn’t exist, so When we execute above program, we get following exception stack trace.
请注意, com.journaldev.MyInvisibleClass
不存在,因此当我们执行上述程序时,将获得以下异常堆栈跟踪。
java.lang.ClassNotFoundException: com.journaldev.MyInvisibleClass
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:292)
at com.journaldev.exceptions.DataTest.main(DataTest.java:7)
In above example, all the three statements will throw java.lang.ClassNotFoundException
.
在上面的示例中,所有这三个语句将抛出java.lang.ClassNotFoundException
。
如何解决ClassNotFoundException (How to resolve ClassNotFoundException)
It’s very easy to fix ClassNotFoundException because the exception stack trace clearly specifies the class not found. Just check for classpath settings and make sure class it’s present at runtime.
修复ClassNotFoundException非常容易,因为异常堆栈跟踪明确指定了未找到的类。 只需检查类路径设置,并确保其在运行时存在即可。
ClassNotFoundException与NoClassDefFoundError (ClassNotFoundException vs NoClassDefFoundError)
NoClassDefFoundError
is a runtime error thrown when a class is not found at runtime. It’s very similar to ClassNotFoundException
. Read more at Java NoClassDefFoundError.
NoClassDefFoundError
是在运行时找不到类时引发的运行时错误。 它与ClassNotFoundException
非常相似。 在Java NoClassDefFoundError上阅读更多内容。
Reference: API Doc
参考: API文档
翻译自: https://www.journaldev.com/20898/java-classnotfoundexception-java-lang-classnotfoundexception