1. Introduction
1.绪论
Both ClassNotFoundException and NoClassDefFoundError occur when the JVM can not find a requested class on the classpath. Although they look familiar, there are some core differences between these two.
当JVM无法在classpath上找到请求的类时,ClassNotFoundException和NoClassDefFoundError都会发生。虽然它们看起来很熟悉,但这两者之间有一些核心区别。
In this tutorial, we’ll discuss some of the reasons for their occurrences and their solutions.
在本教程中,我们将讨论其发生的一些原因及其解决方案。
2. ClassNotFoundException
2.ClassNotFoundException
ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath.
ClassNotFoundException是一个检查过的异常,当应用程序试图通过其完全限定的名称加载一个类,但在classpath上找不到其定义时,就会发生这个异常。
This occurs mainly when trying to load classes using Class.forName(), ClassLoader.loadClass() or ClassLoader.findSystemClass(). Therefore, we need to be extra careful of java.lang.ClassNotFoundException while working with reflection.
这主要发生在试图使用Class.forName()、ClassLoader.loadClass()或ClassLoader.findSystemClass()加载类时。因此,在使用反射时,我们需要格外注意java.lang.ClassNotFoundException。
For example, let’s try to load the JDBC driver class without adding necessary dependencies which will get us ClassNotFoundException:
例如,让我们尝试加载JDBC驱动类而不添加必要的依赖,这将得到ClassNotFoundException:。
@Test(expected = ClassNotFoundException.class)
public void givenNoDrivers_whenLoadDriverClass_thenClassNotFoundException()
throws ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
3. NoClassDefFoundError
3.NoClassDefFoundError
NoClassDefFoundError is a fatal error. It occurs when JVM can not find the definition of the class while trying to:
NoClassDefFoundError是一个致命的错误。它发生在JVM无法找到类的定义而试图。
- Instantiate a class by using the new keyword
- Load a class with a method call
The error occurs when a compiler could successfully compile the class, but Java runtime could not locate the class file. It usually happens when there is an exception while executing a static block or initializing static fields of the class, so class initialization fails.
当编译器可以成功地编译类,但Java运行时无法找到类文件时,就会发生这个错误。它通常发生在执行静态块或初始化类的静态字段时出现异常,所以类初始化失败。
Let’s consider a scenario which is one simple way to reproduce the issue. ClassWithInitErrors initialization throws an exception. So, when we try to create an object of ClassWithInitErrors, it throws ExceptionInInitializerError.
让我们考虑一个场景,这是重现该问题的一个简单方法。ClassWithInitErrors初始化时抛出一个异常。因此,当我们试图创建一个ClassWithInitErrors的对象时,它会抛出ExceptionInitializerError。
If we try to load the same class again, we get the NoClassDefFoundError:
如果我们再次尝试加载同一个类,我们会得到NoClassDefFoundError:。
public class ClassWithInitErrors {
static int data = 1 / 0;
}
public class NoClassDefFoundErrorExample {
public ClassWithInitErrors getClassWithInitErrors() {
ClassWithInitErrors test;
try {
test = new ClassWithInitErrors();
} catch (Throwable t) {
System.out.println(t);
}
test = new ClassWithInitErrors();
return test;
}
}
Let us write a test case for this scenario:
让我们为这种情况写一个测试案例。
@Test(expected = NoClassDefFoundError.class)
public void givenInitErrorInClass_whenloadClass_thenNoClassDefFoundError() {
NoClassDefFoundErrorExample sample
= new NoClassDefFoundErrorExample();
sample.getClassWithInitErrors();
}
4. Resolution
4.决议
Sometimes, it can be quite time-consuming to diagnose and fix these two problems. The main reason for both problems is the unavailability of the class file (in the classpath) at runtime.
有时,诊断和解决这两个问题会相当耗时。这两个问题的主要原因是在运行时无法得到类文件(在classpath中)。
Let’s take a look at few approaches we can consider when dealing with either of these:
让我们看看在处理这两种情况时我们可以考虑的几种方法。
- We need to make sure whether class or jar containing that class is available in the classpath. If not, we need to add it
- If it’s available on application’s classpath then most probably classpath is getting overridden. To fix that we need to find the exact classpath used by our application
- Also, if an application is using multiple class loaders, classes loaded by one classloader may not be available by other class loaders. To troubleshoot it well, it’s essential to know how classloaders work in Java
5. Summary
5.摘要
While both of these exceptions are related to classpath and Java runtime unable to find a class at run time, it’s important to note their differences.
虽然这两种异常都与classpath和Java运行时无法在运行时找到一个类有关,但必须注意它们的区别。
Java runtime throws ClassNotFoundException while trying to load a class at runtime only and the name was provided during runtime. In the case of NoClassDefFoundError, the class was present at compile time, but Java runtime could not find it in Java classpath during runtime.
Java运行时抛出ClassNotFoundException ,同时试图在运行时加载一个类,而该名称是在运行时提供。在NoClassDefFoundError的情况下,类在编译时已经存在,但Java运行时在Java classpath中找不到它。
As always, the complete code for all examples can be found over on GitHub.
一如既往,所有例子的完整代码都可以在GitHub上找到。