由于 NoClassDefFoundError,XStream 无法反序列化 XML
XStream cannot deserialize XML due to NoClassDefFoundError
所以我尝试使用 XStream 库将一个简单对象序列化为 XML 文件。
这些导致以下异常抱怨 class 找不到人。我试过向 Person class 添加 XStream 注释,但这也没有成功。
使用
的 XStream 库(反)序列化对象的正确方法是什么
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:350)
at XMLTest.main(XMLTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
为了完整起见,我在下面添加了我的代码。
import com.thoughtworks.xstream.XStream;
public class XMLTest {
public static void main(String[] args) {
Person john = new Person("John", "Doe", 1234);
XStream xstream = new XStream();
String xml = xstream.toXML(john);
Person newJohn = (Person) xstream.fromXML(xml);
System.out.println("Hi " + newJohn.getFirstname());
}
}
和
public class Person {
private String firstname;
private String lastname;
private int age;
public Person(String firstname, String lastname, int age) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
使用
XStream xstream = new XStream(new StaxDriver());
相反。
解释是如果你这样做
XStream xstream = new XStream(new StaxDriver());
那么你需要 xstream-[version].jar, xpp3-[version].jar 和 xmlpull-[version].jar 在类路径中。”但是 XStream xstream = new XStream(new StaxDriver());
不需要 XPP3以 Java 6
开头的库
一个非常简单的解释和更多信息可以在repo-source
中获得
所以我尝试使用 XStream 库将一个简单对象序列化为 XML 文件。
这些导致以下异常抱怨 class 找不到人。我试过向 Person class 添加 XStream 注释,但这也没有成功。
使用
的 XStream 库(反)序列化对象的正确方法是什么Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:350)
at XMLTest.main(XMLTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
为了完整起见,我在下面添加了我的代码。
import com.thoughtworks.xstream.XStream;
public class XMLTest {
public static void main(String[] args) {
Person john = new Person("John", "Doe", 1234);
XStream xstream = new XStream();
String xml = xstream.toXML(john);
Person newJohn = (Person) xstream.fromXML(xml);
System.out.println("Hi " + newJohn.getFirstname());
}
}
和
public class Person {
private String firstname;
private String lastname;
private int age;
public Person(String firstname, String lastname, int age) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
使用
XStream xstream = new XStream(new StaxDriver());
相反。
解释是如果你这样做
XStream xstream = new XStream(new StaxDriver());
那么你需要 xstream-[version].jar, xpp3-[version].jar 和 xmlpull-[version].jar 在类路径中。”但是 XStream xstream = new XStream(new StaxDriver());
不需要 XPP3以 Java 6
一个非常简单的解释和更多信息可以在repo-source
中获得