maven + eclipse + tomcat:找不到类异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21222978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:00:25  来源:igfitidea点击:

maven + eclipse + tomcat : class not found exception

eclipsemaventomcat

提问by SK176H

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)

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

The war gets deployed to tomcat 7 correctly.

War被正确部署到 tomcat 7。

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.

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

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)

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

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

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.

注意:我可以运行 mvn clean install 并创建 jar 文件。然后当我将 jar 移动到 tomcat(7) webapp 目录并重新启动它时,它工作得很好。

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.

我没有发布 pom.xml 的详细信息,因为应用程序本身运行良好。但是,在分辨率中需要它的 id 我可以发布它。

回答by SK176H

Tomcat & Spring Web - Class Not Found Exception org.springframework.web.context.ContextLoaderListener

Tomcat 和 Spring Web - 找不到类异常 org.springframework.web.context.ContextLoaderListener

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

在 Project Explorer 中右键单击您的 Web 项目 -> 选择“属性”。在项目属性下,选择“部署程序集”。

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.

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

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

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