使用JAXB在OSGi独立框架中获取ClassCastException

我通过运行命令运行OSGi框架,如下所示:

java -jar org.eclipse.osgi_3.6.2.R36x_v20110210.jar -console

我的插件运行正常但在运行我的插件时需要系统库(JavaSE1.6.xx)的JAXB包来解析xml文件我得到异常跟踪如下:

Exception in thread "DummyProgram" java.lang.ExceptionInInitializerError
        at javax.xml.bind.DatatypeConverter.<clinit>(DatatypeConverter.java:78)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$3.run(JAXBContextImpl.java:262)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$3.run(JAXBContextImpl.java:260)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:260)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
        at com.entities.conf.JAXBMTSConfig.unmarshalApps(JAXBMTSConfig.java:113)
        20 more..
Caused by: java.lang.ClassCastException: org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl cannot be cast to javax.xml.datatype.DatatypeFactory
        at javax.xml.datatype.DatatypeFactory.newInstance(Unknown Source)
        at javax.xml.bind.DatatypeConverterImpl.<clinit>(DatatypeConverterImpl.java:742)

最佳答案 我认为与javax.xml.bind.*包的不同版本存在冲突.我猜他们是由系统库和xerces(?)jar导出的?

因此,您需要找出哪个包导出这些包,并解决冲突.

问候,弗兰克

点赞