7

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.

Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238

4 Answers4

11

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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
10

I found the issue:

If you right click on the project and select properties you can set the "Java EE Module dependencies" to include the modules you are using.

Hey Presto it works.

Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
  • 8
    Good job. What happens behind the scenes is that this dependency tells the compiler to place these libraries into the WEB-INF/lib directory of your deployment (probably a WAR file).

    When the application loads on the app server - this path is included by default on the web application's classpath and your app can now "see" these class libraries.
    – Vinnie May 13 '09 at 12:58
  • 6
    Note that this feature is now called "Deployment Assembly" see http://stackoverflow.com/questions/3365369/java-ee-module-dependencies-in-web-project – GuiSim Dec 22 '11 at 14:52
9

Java EE Module Dependencies seem to be the way to go. However, menus names changed in Eclipse Helios: that particular menu is now under "Deployment Assembly".

Florent
  • 186
  • 1
  • 8
0

A easy mistake to make is to misname the directory under WEB_INF, I've named it libs instead of lib, compiles fine but tomcat throws errors.