繁体   English   中英

ClassCastException:org.apache.xerces.jaxp.DocumentBuilderFactoryImpl无法转换为javax.xml.parsers.DocumentBuilderFactory

[英]ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory

这个问题与关于Xerces依赖地狱的许多讨论有关,但我似乎无法解决。

我正在尝试将LaTeX代码导出为Java中的PDF。 我的代码是Cytoscape 3.4的OSGI软件包的一部分,并由Maven管理和构建。 LaTeX库是jlatexmath (1.0.6),要写入SVG和PDF,我想尝试apache fop (0.95)库。

Fop依赖于整个batik库,而batik库又依赖于xml-apis (1.3.04)。

包含xml-apis ,出现此错误:

java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
        at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
        at org.scilab.forge.jlatexmath.TeXFormulaSettingsParser.<init>(TeXFormulaSettingsParser.java:74)

这是可以理解的,因为DocumentBuilderFactory的实现应该是JRE中包含的com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

当我排除xml-apis和xml-apis-ext库时,出现另一个错误:

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>fop</artifactId>
    <version>0.95</version>
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis-ext</artifactId>
        </exclusion>
    </exclusions>
</dependency>


java.lang.ClassNotFoundException: org.w3c.dom.Document not found by... [115]
        at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
        at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
        at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

这很奇怪,因为org.w3c.dom.Document也应该位于JRE中。

因此,如何强制Java使用Java中包含的类,而不是查找从Maven依赖项加载的类?

您的软件包需要导入软件包org.w3c.dom 您必须导入捆绑软件依赖的所有软件包 ,但以java.开头的软件包除外java. ,例如java.langjava.net等。

由于Neil Bartlett建议不导入org.w3c.dom ,所以我设法解决了这个问题(尽管我仍然不确定为什么“ import *”不够用)。

在maven-bundle-plugin的配置说明中,我将导入更改为:

<Import-Package>org.w3c.dom,*;resolution:=optional</Import-Package>

另外,不应排除xml-apis-ext ,因此fop依赖关系变为:

<dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>fop</artifactId>
            <version>0.95</version>
            <exclusions>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

然后,为了完整起见,fop工件似乎需要avalon-framework-impl一个类,该类在maven依赖项中不存在,因此我将其单独添加,同时排除了所有尝试包括xml-apis新尝试:

<dependency>
            <groupId>avalon-framework</groupId>
            <artifactId>avalon-framework-impl</artifactId>
            <version>4.3</version>
            <exclusions>
            <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xmlParserAPIs</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM