本文共 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无法将其加载到内存中。
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
.
如您所见,它的超类是Throwable
和Error
。
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
类,如异常堆栈跟踪中清楚显示的那样。 下图显示了以上所有命令,并在终端窗口中输出。
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错误:
pankaj:temp pankaj$ java -classpath .:.. DataTestData Id = 10pankaj:temp
Remember that earlier I had moved Data class to previous directory.
请记住,之前我已经将Data类移到了先前的目录。
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/