博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.lang.NoClassDefFoundError
阅读量:2538 次
发布时间:2019-05-11

本文共 5292 字,大约阅读时间需要 17 分钟。

java.lang.NoClassDefFoundError is runtime error thrown when a required class is not found in the classpath and hence JVM is unable to load it into memory.

java.lang.NoClassDefFoundError是在类路径中找不到所需的类时抛出的运行时错误,因此JVM无法将其加载到内存中。

java.lang.NoClassDefFoundError (java.lang.NoClassDefFoundError)

  • NoClassDefFoundError is a runtime error, so it’s beyond our application scope to anticipate and recover from this.

    NoClassDefFoundError是运行时错误,因此超出预期范围并从中恢复是超出了我们的应用范围。
  • java.lang.NoClassDefFoundError is a runtime error, it never comes in compile time.

    java.lang.NoClassDefFoundError是运行时错误,它永远不会在编译时出现。
  • It’s very easy to debug NoClassDefFoundError because it clearly says that JVM was unable to find the required class, so check classpath configurations to make sure required classes are not missed.

    调试NoClassDefFoundError非常容易,因为它明确指出JVM无法找到所需的类,因此请检查类路径配置以确保不会丢失所需的类。

NoClassDefFoundError类图 (NoClassDefFoundError Class Diagram)

Below image shows NoClassDefFoundError class diagram and it’s super classes.

下图显示了NoClassDefFoundError类图及其超类。

As you can see that it’s super classes are Throwable and Error.

如您所见,它的超类是ThrowableError

java.lang.NoClassDefFoundError原因 (java.lang.NoClassDefFoundError Reasons)

Let’s first try to replicate a scenario where we get NoClassDefFoundError at runtime. Let’s say we have a java classes like below.

让我们首先尝试复制一个在运行时遇到NoClassDefFoundError的场景。 假设我们有一个类似下面的java类。

public class Data {	private int id;	public int getId() {		return id;	}	public void setId(int id) {		this.id = id;	}	}

Notice that above class doesn’t depend on any other custom java classes, it just uses java built-in classes. Let’s create another class that will use Data class in the same directory.

注意,上述类不依赖于任何其他自定义Java类,它仅使用Java内置类。 让我们创建另一个将在同一目录中使用Data类的类。

public class DataTest {	public static void main(String[] args) {		Data data = new Data();		data.setId(10);		System.out.println("Data Id = "+data.getId());	}}

Now let’s compile DataTest class and then execute it like below.

现在,让我们编译DataTest类,然后像下面一样执行它。

pankaj:temp pankaj$ lsData.java	DataTest.javapankaj:temp pankaj$ javac DataTest.java pankaj:temp pankaj$ ls Data.class	Data.java	DataTest.class	DataTest.javapankaj:temp pankaj$ java DataTestData Id = 10pankaj:temp pankaj$

So far everything is fine, now let’s move Data class files to somewhere else and then try to execute DataTest class. We will not compile it again since then it will give compilation error.

到目前为止,一切都很好,现在让我们将Data类文件移动到其他地方,然后尝试执行DataTest类。 从那时起,我们将不再编译它,否则它将给出编译错误。

pankaj:temp pankaj$ mv Data.java Data.class ../pankaj:temp pankaj$ lsDataTest.class	DataTest.javapankaj:temp pankaj$ java DataTestException in thread "main" java.lang.NoClassDefFoundError: Data	at DataTest.main(DataTest.java:5)Caused by: java.lang.ClassNotFoundException: Data	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)	... 1 morepankaj:temp pankaj$

Here it is, we got NoClassDefFoundError because java runtime is unable to find Data class as clearly shown in the exception stack trace. Below image shows all the above commands and output in the terminal window.

在这里,我们得到了NoClassDefFoundError因为java运行时无法找到Data类,如异常堆栈跟踪中清楚显示的那样。 下图显示了以上所有命令,并在终端窗口中输出。

如何解决java.lang.NoClassDefFoundError? (How to resolve java.lang.NoClassDefFoundError?)

From above example, we can clearly identify that the only reason for this error is that the required classes were available at compile time but not at runtime. You can fix NoClassDefFoundError error by checking following:

从上面的示例中,我们可以清楚地确定此错误的唯一原因是所需的类在编译时可用,而在运行时不可用。 您可以通过检查以下内容来修复NoClassDefFoundError错误:

  • Check the exception stack trace to know exactly which class throw the error and which is the class not found by java.

    检查异常堆栈跟踪以确切地知道哪个类引发了错误,以及哪个类不是Java找不到的类。
  • Next step is to look for classpath configuration, sometimes we compile our classes in Eclipse or some other environment and run in some other environment and we can miss classpath configurations. For example, I can fix above issue easily by adding the directory which contains Data class to the classpath like below.
    pankaj:temp pankaj$ java -classpath .:.. DataTestData Id = 10pankaj:temp

    Remember that earlier I had moved Data class to previous directory.

    请记住,之前我已经将Data类移到了先前的目录。

  • Most of the times, NoClassDefFoundError comes with applications running on some server as or web services, in that case check if the required jars are part of the WAR file or not. For example, below maven configuration will not package jar file when generating WAR file.
    javax.servlet
    servlet-api
    3.0.1
    provided

    But we need it for creating a based web application, usually this jar is always part of Tomcat or any other application server.

    或Web服务在某些服务器上运行的 ,在这种情况下,请检查所需的jar是否属于WAR文件。 例如,以下Maven配置在生成WAR文件时不会打包jar文件。

    但是我们需要它来创建基于的Web应用程序,通常,此jar始终是Tomcat或任何其他应用程序服务器的一部分。

That’s all for a quick look at java.lang.NoClassDefFoundError, I hope you find enough idea when this error comes and how to fix it easily.

这就是快速浏览java.lang.NoClassDefFoundError的全部,希望当此错误发生时以及如何轻松修复它时,您找到足够的主意。

Reference: ,

参考: ,

翻译自:

转载地址:http://jrmzd.baihongyu.com/

你可能感兴趣的文章
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>