Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven + eclipse + tomcat : class not found exception

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)

The war gets deployed to tomcat 7 correctly.

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)

is there any specific class path setting that needs to be done in the 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.

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.

like image 453
SK176H Avatar asked Jan 19 '14 21:01

SK176H


1 Answers

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

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

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.

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

like image 99
SK176H Avatar answered Oct 21 '22 23:10

SK176H