Java – Under Tomcat java.lang.NoClassDefFoundError when accessing a servlet


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:

    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at java.lang.ClassLoader.loadClassInternal(
    at SecurePaymentAjaxData.doPost(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at org.apache.coyote.http11.Http11Processor.process(
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(

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

XStream xstream = new XStream(new JettisonMappedXmlDriver());
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.

Best Solution

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.

Related Question