在网上查阅的相关代码,基本都一样,试过都报这个错,请高手指点
代码:
public static void toXML(Object bean) {
FileOutputStream xmlData = null;
JOXBeanOutputStream joxOut = null;
try {
xmlData = new FileOutputStream( "d:/bean.xml ");
joxOut = new JOXBeanOutputStream(xmlData, "UTF-8 ");//这句报错
joxOut.writeObject( "person ", bean);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
try {
xmlData.close();
joxOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误信息:
java.lang.NullPointerException
at com.jox.main.JoxMain.toXML(JoxMain.java:56)
at com.jox.main.JoxMain.main(JoxMain.java:79)
Exception in thread "main " java.lang.NoClassDefFoundError: org/apache/xml/serialize/OutputFormat
at com.wutka.jox.JOXBeanOutputStream.setupSink(JOXBeanOutputStream.java:173)
at com.wutka.jox.JOXBeanOutputStream. <init> (JOXBeanOutputStream.java:82)
at com.jox.main.JoxMain.toXML(JoxMain.java:49)
at com.jox.main.JoxMain.main(JoxMain.java:79)
Caused by: java.lang.ClassNotFoundException: org.apache.xml.serialize.OutputFormat
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 4 more
jar包打了,代码是不报错的,运行报错
缺少OutputFormat 类,缺少jar包 org.apache.xml.serialize.OutputFormat到http://archive.apache.org/dist/xml/xerces-j/站点上去下载Xerces-J-bin.2.6.0.zip。 org.apache.xml.serialize在xercesImpl.jar里面!