本文介绍了maven + eclipse + tomcat:找不到类异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用servlet的简单Web应用程序.我将其创建为Maven项目,然后将其导入eclipse(文件-> import->现有的maven项目)

I have a simple web app using that uses servlets. I created it as a maven project and then imported it into eclipse ( file ->import -> existing maven projects)

战争正确地部署到了tomcat 7.

The war gets deployed to tomcat 7 correctly.

但是,当我在Eclipse中创建tomcat 7服务器,然后在eclipse中启动tomcat时,运行应用程序时会出现异常.

However when I create a tomcat 7 server in Eclipse and then start tomcat in eclipse I get an exception when I run the application.

root cause

java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
    first.model.BeerExpert.<clinit>(BeerExpert.java:12)
    first.servlet.BeerServlet.doPost(BeerServlet.java:26)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    first.model.BeerExpert.<clinit>(BeerExpert.java:12)
    first.servlet.BeerServlet.doPost(BeerServlet.java:26)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

是否需要在eclipse/tomcat中完成任何特定的类路径设置

is there any specific class path setting that needs to be done in the eclipse/tomcat

注意:我能够运行mvn clean install并创建jar文件.然后,当我将jar移到tomcat(7)webapp目录并重新启动时,它就可以正常工作.

Note: I am able to run mvn clean install and it creates the jar file. And then when I move the jar to a tomcat(7) webapp directory and restart it works just fine.

我没有发布pom.xml的详细信息,因为应用程序本身运行正常.无论如何,我都可以发布它来解决它.

I am not posting details of the pom.xml because the application itself is working fine. However id it is needed in resolution I can post it.

推荐答案

右键单击项目浏览器中的Web项目->选择属性".在项目属性下,选择部署程序集".

Right Click on your web project in Project Explorer -> select 'Properties'. Under project properties, select 'Deployment Assembly'.

Deployment Assembly属性页面显示将由Eclipse作为组合工件发布到服务器的内容.您需要告诉Eclipse您也要发布所有Maven依赖项.

The Deployment Assembly property page shows the content that will be published as a assembled artifact by Eclipse to the server. You need to tell Eclipse that you want all your Maven dependencies to be published too.

为此,请单击添加"按钮,然后选择"Java构建路径条目".单击下一步",然后选择"Maven依赖关系".当Eclipse将项目发布到WST服务器时,这会将Maven依赖关系JAR文件发布到lib文件夹中

To do that, click 'Add' button, then select 'Java Build Path Entries'. Click Next and select Maven Dependencies. This will publish the Maven dependency JAR files to the lib folder when Eclipse publishes your project to WST server

这篇关于maven + eclipse + tomcat:找不到类异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:22