Under Tomcat java.lang.NoClassDefFoundError when accessing a servlet?

Omar Kooheji picture Omar Kooheji · May 13, 2009 · Viewed 23.4k times · Source

I'm writing a web applicaion in Eclipse, and I'm using the XStream Serialization Library to generate My JSON.

I've encapsulated the Code that builds up the JSON in a class which is called by my servelet. Then encapsulated class has a main method for testing and when I run it everything seems to work fine.

However when I use the call the class from my servelet I get a java.lang.NoClassDefFoundError error, sayying that I've not loaded the XStream libraries. I assume I've got my build path wrong, but I've set the XStream libraries to be in the build path for the project, so as far as I know it should work.

What is likely to be going wrong here?

Following is the exact exception:

java.lang.ClassNotFoundException: com.thoughtworks.xstream.io.HierarchicalStreamDriver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at SecurePaymentAjaxData.doPost(SecurePaymentAjaxData.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

And here is the relavant code that uses the xstream class:

XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("CallDataUpdate", CallDataUpdate.class);
xstream.alias("CardStatus", CardStatus.class);
String jsonData = xstream.toXML(updateData);

I stress that this code works fine when run as a java application, I'm sure it's something to do with loading the libraries, I just don't know where I've gone wrong.

Answer

Brian Agnew picture Brian Agnew · May 13, 2009

Don't forget that there's a difference between how you build and how you deploy. That is, you may be building against XStream, but you should also package it in the .war file for deployment.

Explode your resultant .war file (jar tvf {filename} to see the contents) and check whether it's there (a .jar file under WEB-INF/lib). I suspect it's not.