部署的 Tomcat 7 WAR 应用程序抛出 NoClassDefFoundError 错误
我正在开发一个 hello-world 类型的 Web 应用程序。我使用 Eclipse Helios 作为 IDE,使用 Maven 2 来处理依赖项,并使用本地运行的 Tomcat 7 服务器来部署和测试。该应用程序基于 Spring MVC,并使用 Hibernate 进行数据管理。
我可以成功地将项目打包成WAR文件。当我在Tomcat中部署WAR时,日志中报告以下错误:
严重:上下文初始化失败
org.springframework.beans.factory.BeanCreationException: 使用名称创建 bean 时出错 'hibernateSessionFactory' 定义于 ServletContext 资源 [/WEB-INF/spring-servlet.xml]: 调用init方法失败; 嵌套异常是 java.lang.NoClassDefFoundError:可以 不初始化类 org.hibernate.cfg.AnnotationConfiguration 在
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 等等...
我检查以确保正确的依赖项包含在我的 POM 中,它是:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
<type>jar</type>
<optional>false</optional>
</dependency>
我检查了一下发现打包的 WAR 文件具有所需的 JAR 文件,该文件具有错误中提到的类 (org.hibernate.cfg.AnnotationConfiguration),并且它执行以下操作:
./Tomcat 7/webapps/helloworld/WEB-INF/lib/hibernate-annotations-3.4.0.GA.jar
根据此信息,我认为这可能是类路径配置问题。从我阅读的有关 Tomcat 7 的文档来看,Tomcat 应该能够利用“/WEB-INF/lib”文件夹中的 JAR 文件。有人知道我是否跳过了 Tomcat 配置中的一个步骤,以便它识别 web 应用程序的 lib 文件夹中的 JAR 文件吗?
I am working on a hello-world type of web application. I am using Eclipse Helios as my IDE, Maven 2 to handle dependencies, and a locally run Tomcat 7 server to deploy and test. The application is Spring MVC based and uses Hibernate for data management.
I can successfully package the project into a WAR file. When I deploy the WAR in Tomcat, the following error is reported in the logs:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'hibernateSessionFactory' defined in
ServletContext resource
[/WEB-INF/spring-servlet.xml]:
Invocation of init method failed;
nested exception is
java.lang.NoClassDefFoundError: Could
not initialize class
org.hibernate.cfg.AnnotationConfiguration
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) Etc...
I checked to be sure that the correct dependency is included in my POM, and it is:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
<type>jar</type>
<optional>false</optional>
</dependency>
I checked to see that the packaged WAR file has the required JAR file which has the class mentioned (org.hibernate.cfg.AnnotationConfiguration) in the error and it does:
./Tomcat 7/webapps/helloworld/WEB-INF/lib/hibernate-annotations-3.4.0.GA.jar
Based on this information, I figure that this might be a classpath configuration issue. From the documentation I've read on Tomcat 7, Tomcat should be able to utilize the JAR files in "/WEB-INF/lib" folder. Any one know if I may have skipped a step in configuration of Tomcat in order for it to recognize the JAR files in the lib folder of a webapp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到这是通用建议,但我过去在 Tomcat 和 Jetty 上都遇到过类似的问题,这是因为我有某些依赖项的多个/错误版本。我首先使用 Eclipse 中的 M2Eclipse POM 编辑器,单击“依赖关系层次结构”,然后仔细检查所有传递依赖关系。
I realize this is generic advice, but I've had similar problems like this in the past with both Tomcat and Jetty and it was because I had multiple/wrong versions of some dependency. I'd start by using the M2Eclipse POM editor in Eclipse and click on "Dependency Hierarchy" and carefully review all of the transitive dependencies.