我的Java Web应用程序中的ClassNotFoundException / NoClassDefFoundError [英] ClassNotFoundException/NoClassDefFoundError in my Java web application

查看:189
本文介绍了我的Java Web应用程序中的ClassNotFoundException / NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java开发Web应用程序。当我将它部署到我的应用程序服务器(Jetty,Tomcat,JBoss,GlassFish等)时会抛出错误。我可以在stacktrace中看到此错误消息:

I developer a web application using Java. When I deploy it to my application server (Jetty, Tomcat, JBoss, GlassFish, etc.) throws an error. I can see this error message in the stacktrace:

java.lang.ClassNotFoundException

java.lang.NoClassDefFoundError

这是什么意思,我该如何解决?

What does this mean and how can I fix it?

推荐答案


这是什么意思?

What does this mean?

首先,让我们看看 java的含义。 lang.ClassNotFoundException

First, let's see the meaning of java.lang.ClassNotFoundException:


当应用程序尝试通过其字符串加载类时抛出名称使用:

Thrown when an application tries to load in a class through its string name using:


  • 类中的 forName 方法

  • ClassLoader findSystemClass 方法>。

  • ClassLoader 中的 loadClass 方法。

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

但是找不到具有指定名称的类的定义。

but no definition for the class with the specified name could be found.

通常,当尝试以这种形式手动打开连接时会发生这种情况:

Usually, this happens when trying to open a connection manually in this form:

String jdbcDriver = "...'; //name of your driver
Class.forName(jdbcDriver);

或者当你引用一个属于外部库的类而且奇怪地当应用程序服务器尝试部署应用程序时,无法加载此类。

Or when you refer to a class that belongs to an external library and strangely this class cannot be loaded when the application server tries to deploy the application.

让我们看看 java.lang.NoClassDefFoundError (强调我的):

Let's see the meaning of java.lang.NoClassDefFoundError (emphasis mine):


如果Java虚拟机或 ClassLoader 实例尝试加载,则抛出类的定义(作为普通方法调用的一部分或作为使用新表达式创建新实例的一部分),并且找不到类的定义。

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

在编译当前正在执行的类时存在搜索的类定义,但无法再找到定义

最后一部分说明了一切:该类存在于编译时,即wh我通过我的IDE编译了应用程序,但它在运行时不可用,即部署应用程序时。

The last part says it all: the class existed at compile time i.e. when I compiled the application through my IDE, but it is not available at runtime i.e. when the application is deployed.


我该如何解决?

how can I fix it?

在Java Web应用程序中,应用程序使用的所有第三方库必须去在WEB-INF / lib文件夹中。确保将所有必需的库(罐)放在那里。您可以轻松查看:

In Java web applications, all third party libraries used by your application must go in WEB-INF/lib folder. Make sure that all the necessary libraries (jars) are placed there. You can check this easily:

- <webapp folder>
  - WEB-INF
    - lib
      + jar1
      + jar2
      + ...
  - META-INF
  - <rest of your folders>

此问题通常出现在JDBC连接jar(MySQL,Derby,MSSQL,Oracle等)或web MVC框架库,如JSF或Spring MVC。

This problem usually arises for JDBC connectivity jars (MySQL, Derby, MSSQL, Oracle, etc.) or web MVC frameworks libraries like JSF or Spring MVC.

考虑到某些第三方库依赖其他第三方库,所以你有在WEB-INF / lib中添加所有以使应用程序正常工作。一个很好的例子是RichFaces 4库,您必须手动下载和添加外部库

Take into account that some third party libraries rely on other third party libraries, so you have to add all of them in WEB-INF/lib in order to make the application work. A good example of this is RichFaces 4 libraries, where you have to download and add the external libraries manually.

注意 Maven 用户:除非您将库设置为提供 test ,否则不应遇到这些问题或系统。如果设置为提供,则您负责在类路径中的某处添加库。您可以在此处找到有关依赖关系范围的更多信息:依赖关系简介机制

Note for Maven users: you should not experience these problems unless you have set the libraries as provided, test or system. If set to provided, you're responsible to add the libraries somewhere in the classpath. You can find more info about the dependency scopes here: Introduction to the Dependency Mechanism

如果必须在应用程序服务器上部署的多个应用程序之间共享库,例如两个应用程序的MySQL连接器,还有另一种选择。不是使用自己的MySQL连接器库部署两个war文件,而是将此库放在服务器应用程序的公共库文件夹中,这将使库能够位于所有已部署应用程序的类路径中。

In case the library must be shared among several applications that will be deployed on your application server e.g. MySQL connector for two applications, there's another alternative. Instead of deploying two war files each with their own MySQL connector library, place this library in the common library folder of the server application, this will enable the library to be in the classpath of all the deployed applications.

此文件夹与应用程序服务器不同。

This folder vary from application server.


  • Tomcat 7/8:< tomcat_home> / lib

  • JBoss 7 / Wildfly:< jboss_home> / standalone / lib

  • Tomcat 7/8: <tomcat_home>/lib
  • JBoss 7/Wildfly: <jboss_home>/standalone/lib

这篇关于我的Java Web应用程序中的ClassNotFoundException / NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆