10

I'm trying to use XStream. I've added the XStream executable Jar file to my project. Executing the following command:

    XStream xstream = new XStream();

Is resulting in the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException

at com.thoughtworks.xstream.XStream.<init>(XStream.java:350)
at xstream_test.XmlTrasformer.objectToXml(XmlTrasformer.java:56)
at xstream_test.XmlTrasformer.main(XmlTrasformer.java:31)

Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more

Any idea I might have done wrong? Thanks.

user3062233
  • 179
  • 1
  • 2
  • 7

4 Answers4

18

Make sure you have included all the jars that come with XStream also specially "kxml2.jar" and "xmlpull-1.1.3.1.jar" file. Jar version may deffer incase of yours.

A Paul
  • 8,113
  • 3
  • 31
  • 61
  • What do you mean by "all the jars that come with XStream"? The download from their website gave me only one file. – user3062233 Feb 09 '14 at 20:31
  • I mean xstream dependency jars. – A Paul Feb 10 '14 at 03:29
  • 3
    @user3062233 I had a similar problem as yours and installed the xmlpull jar, and it worked. The website has a list of "Optional Dependencies" here: http://xstream.codehaus.org/download.html#optional-deps – Ian Hunter Mar 05 '14 at 03:58
  • Importing the 2 jars mentioned in your answer resolved my issue. Thanks! – coder0h1t Apr 05 '17 at 01:24
11

Use

new XStream(new StaxDriver())

xpp and xmlpull are very old codebases

with non-default constructor you could avoid those 2 jars

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
  • They may be old, but not necessarily worse. In my tests on a real production data StaxDriver is about 10% slower that default XStream's XppDriver on serialization and deserialization. – Sergio Jul 05 '15 at 10:44
  • 1
    Thank you so much! This saved me a heap of problems! – Jack Feb 12 '16 at 11:42
  • glad i was able to help, my company has strict control over what jars can be used in production, and these tricks have earned me good praise – Kalpesh Soni Feb 14 '16 at 19:40
1

You can too use :

new XSteam(new DomDriver())

The difference with StaxDriver is the output of convert objet to xml.

Output DomDriver :

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

Output StaxDriver :

<?xml version="1.0" ?><person><firstname>Joe</firstname><lastname>Walnes</lastname><phone><code>123</code><number>1234-456</number></phone><fax><code>123</code><number>9999-999</number></fax></person>
Boostaut
  • 11
  • 2
0

Not sure if this may help someone right now.

I had a nightmare with Jmeter maven plugin, with the same issue, and I found out that the dependency below solved the issue.

    <dependency>
        <groupId>xmlpull</groupId>
        <artifactId>xmlpull</artifactId>
        <version>1.1.3.1</version> <--- or whatever
    </dependency>

Probably this takes all the required in one row.

Not sure why the maven jmeter plugin, doesn't come along with all the required dependencies.

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32