ClassNotFoundException DispatcherServlet when launching Tomcat (Maven dependencies not copied to wtpwebapps)
Asked Answered
J

4

25

I've run into this problem a few times and only just stumbled upon a (semi-) reliable solution, so thought I would post it here in case it helps anyone else, or in case I forget it, and also to ask if anyone has any idea what the underlying cause is and why my fix works.

The problem - I have a spring 3 webapp that I want to use the Run on Server option in Eclipse to run on a Tomcat 7 server. With some projects this just works, but some seem to get in a broken state whereby every time the server starts up I get a class not found: org.springframework.web.servlet.DispatcherServlet.

After some investigation I found out that the project's maven dependencies had not been copied to WEB-INF/lib in ${PROJECT_LOCATION}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/

I tried a lot of things before finally noticing that the project had a warning in the Problems panel:

Classpath entry org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER 
will not be exported or published. Runtime ClassNotFoundExceptions may result.

Perhaps I should have noticed this sooner.

Anyway, selecting this and performing the "Quick Fix" fixes the issue and I can happily run my project. The quick fix is to: "Mark the associated raw classpath entry as a publish/export dependency".

Trouble is, although this works, I don't know what it means. I can't see any obvious changes in my project configuration after it's done. I would quite like to know what the actual problem is and if I can avoid it happening again. I would also like to know if there is something I could have done myself (other than performing the quick fix) that would have fixed it, because I feel like I tried pretty much everything else.

Thanks for your help.

Juxon answered 23/1, 2011 at 22:34 Comment(0)
P
34

Take a look at "Deployment Assembly" section under project properties. The content of this page specify how your project is to be assembled into a running application. In particular, this is the place where you can control which of the libraries that your project references, should be packaged with it. Select Add -> Java Build Path Entries. You will be able to pick among your existing build path entries to include in the assembly.

The quick fix that you found did the same thing for you. If you are curious about what happens under the covers, take a look at your .classpath file before and after the quick fix. The quick fix tags the classpath entry with some assembly information.

Perusse answered 24/1, 2011 at 0:16 Comment(3)
Thanks! I find it impossible to remember stuff if I don't understand how it works, so this will most likely save me a great deal of pain in future.Juxon
Awesome. This really helped me after my Eclipse suddenly exploded for no reason yesterday.Ungodly
You might have to restart the tomcat server after this.Toehold
H
2

Check .class file and replace the following entry

    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER" />

with Below:-

    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">
      <attributes>
        <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
      </attributes>
    </classpathentry>

It worked for me.

Herriott answered 26/9, 2011 at 10:45 Comment(2)
It's unlikely that you would want to export the entire Maven classpath as this will likely result in conflicts on the application server that provides some jars by default (like for instance servlet-api-2.5.jar, jsp-api-2.1.jar, jstl-1.2.jar). Instead each jar should be selected one by one in the Deployment Assembly section of Eclipse as mentioned in the accepted answer by Konstantin.Street
For me <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> is already present, but still I am getting NoClassDefFoundError, , but when I go to the location .metadata/.plugins/org.eclipse.wst.server.core/tmp0, I do not find my maven dependency in Lib, any hints?Alee
R
1

It seems, you have to additionally install Maven Integration for WTP plugin. Its description can be found at issues.sonatype.org.

Reservation answered 31/8, 2011 at 8:35 Comment(0)
U
0

I ran into this problem today, on a project that had been working for a long time. I tried all of the above solutions, but (1) I wasn't seeing the warning in the Problems panel as the poster had; (2) my Deployment Assembly Java Build Path Entries was not showing anything else available to deploy; (3) the .classpath entry for org.eclipse.jst.component.dependency was already there; and (4) I already had Maven Integration for WTP installed. I finally removed my project from the Tomcat server and then re-deployed it, and the problem disappeared.

Underfoot answered 20/8, 2018 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.