XStream XmlPullParserException
Asked Answered
H

4

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.

Helotry answered 9/2, 2014 at 19:59 Comment(2)
Are you using an IDE (Eclipse, NetBeans, ...) or are you compiling by yourself...?Lucilius
If any answer below helped resolve your issue, please accept it as answer.Fourposter
A
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.

Antidote answered 9/2, 2014 at 20:2 Comment(4)
What do you mean by "all the jars that come with XStream"? The download from their website gave me only one file.Helotry
I mean xstream dependency jars.Antidote
@Helotry I had a similar problem as yours and installed the xmlpull jar, and it worked. The website has a list of "Optional Dependencies" here: xstream.codehaus.org/download.html#optional-depsIcarian
Importing the 2 jars mentioned in your answer resolved my issue. Thanks!Fourposter
C
11

Use

new XStream(new StaxDriver())

xpp and xmlpull are very old codebases

with non-default constructor you could avoid those 2 jars

Countermine answered 7/4, 2015 at 18:57 Comment(3)
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.Pleuropneumonia
Thank you so much! This saved me a heap of problems!Ply
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 praiseCountermine
H
2

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>
Handspike answered 1/3, 2016 at 10:40 Comment(2)
you can always do xstream.marshal(object, new PrettyPrintWriter(writer))Countermine
Stax is more efficient then domCountermine
D
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.

Dealfish answered 29/1, 2021 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.