2014-10-01 232 views
0

我试图序列化第三方类(它不是可序列化的),并且我正在使用xstream来这样做。Java-使用Xstream序列化不可序列化的对象

起初,我试图建立一些类,看看它是否顺利,一切工作正常,但是当我试图将第三方实例转换为xml它thorws异常。

代码:

import java.util.List; 
import java.util.Set; 
import java.io.ByteArrayInputStream; 
import java.io.InputStream; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.PrintStream; 
import java.io.*; 
import java.util.*; 
import java.util.logging.*; 
import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.io.xml.DomDriver; 
import vohmm.application.SimpleTagger3; 

public class Tagger{ 

    public static void main(String[] args) { 
     try { 
      SimpleTagger3 tagger = new SimpleTagger3(args[0]); 
      XStream xStream = new XStream(new DomDriver()); 

      String tagger_xml = xStream.toXML(tagger); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    } 
} 

例外:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sleepycat/je/DatabaseException 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2570) 
    at java.lang.Class.getDeclaredMethod(Class.java:2002) 
    at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.getMethod(SerializationMethodInvoker.java:164) 
    at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.getMethod(SerializationMethodInvoker.java:148) 
    at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.supportsReadObject(SerializationMethodInvoker.java:105) 
    at com.thoughtworks.xstream.converters.reflection.SerializableConverter.isSerializable(SerializableConverter.java:107) 
    at com.thoughtworks.xstream.converters.reflection.SerializableConverter.canConvert(SerializableConverter.java:103) 
    at com.thoughtworks.xstream.core.DefaultConverterLookup.lookupConverterForType(DefaultConverterLookup.java:56) 
    at com.thoughtworks.xstream.XStream$1.lookupConverterForType(XStream.java:498) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:48) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43) 
    at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:82) 
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37) 
    at com.thoughtworks.xstream.XStream.marshal(XStream.java:1022) 
    at com.thoughtworks.xstream.XStream.marshal(XStream.java:1011) 
    at com.thoughtworks.xstream.XStream.toXML(XStream.java:984) 
    at com.thoughtworks.xstream.XStream.toXML(XStream.java:971) 
    at NewDemo.main(NewDemo.java:51) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 
Caused by: java.lang.ClassNotFoundException: com.sleepycat.je.DatabaseException 
    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) 
    ... 56 more 

是否有resone为什么XStream的不会与SimpleTagger3工作?有没有其他的方式来导出java对象?

+0

看着踪迹我会说你错过了依赖。 – user2504380 2014-10-01 10:01:01

回答

0

您的类加载器无法加载名为“com.sleepycat.je.DatabaseException”的类。 确保此类可用。尝试Class.forName("com.sleepycat.je.DatabaseException")以测试课程是否可用。 如果没有将它添加到你的类路径中或使用ClassLoader加载它。

+0

如何使用ClassLoader加载它?我在哪里可以找到那个班(sleepycat)? – tal 2014-10-01 11:26:43

+0

我发现这个类是'Berkeley DB Java版'的一部分,试图找到这个项目的.jar文件。你可以用'new URLClassLoader(new URL [] {new URL(“”)})。loadClass(“”)加载它们,在某些情况下你只需要创建一个新的类加载器。 – kaetzacoatl 2014-10-01 13:09:03