Java ClassCastException: org.apache.xerces.parsers.XIncludeAwareParserConfiguration 无法转换为 org.apache.xerces.xni.parser.XMLParser

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24805360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:55:39  来源:igfitidea点击:

ClassCastException: org.apache.xerces.parsers.XIncludeAwareParserConfiguration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration

javaeclipsegwtxercesjdom

提问by ejoftheweb

I am developing a GWT application in Eclipse and use jdom2 to read some custom xml property files.

我正在 Eclipse 中开发 GWT 应用程序并使用 jdom2 读取一些自定义 xml 属性文件。

Following a recent update my application now fails with the above error when trying to read the xml file. The relevant stack trace is:

在最近的更新之后,我的应用程序现在在尝试读取 xml 文件时失败并出现上述错误。相关的堆栈跟踪是:

org.apache.xerces.parsers.XIncludeAwareParserConfiguration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration
org.apache.xerces.parsers.SAXParser.<init>(Unknown Source)
org.apache.xerces.parsers.SAXParser.<init>(Unknown Source)
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.<init>(Unknown Source)
org.apache.xerces.jaxp.SAXParserImpl.<init>(Unknown Source)
org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Source)
org.jdom2.input.sax.XMLReaders.createXMLReader(XMLReaders.java:165)
org.jdom2.input.SAXBuilder.createParser(SAXBuilder.java:871)
org.jdom2.input.SAXBuilder.buildEngine(SAXBuilder.java:854)
org.jdom2.input.SAXBuilder.getEngine(SAXBuilder.java:904)
org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116)
uk.co.platosys.db.jdbc.DatabaseProperties.loadProperties(DatabaseProperties.java:78)

Researching this problem suggests that the error can arise when incompatible versions of the xerces jars exist on the classpath.

研究此问题表明,当类路径上存在不兼容的 xerces jar 版本时,可能会出现错误。

gwt-dev-2.6.1.jar contains the xerces packages and my hunch is that this latest version of gwt-dev has bundled a version that is incompatible. However jdom2.0.5, the current release, is released with the 2.11 version of Xerces which seems to be the latest released by Apache. Putting these jars on my classpath doesn't seem to resolve matters; I have previously been able to rely on the versions in gwt-dev.

gwt-dev-2.6.1.jar 包含 xerces 包,我的预感是这个最新版本的 gwt-dev 捆绑了一个不兼容的版本。然而,当前版本 jdom2.0.5 与 2.11 版本的 Xerces 一起发布,这似乎是 Apache 发布的最新版本。将这些 jars 放在我的类路径上似乎并不能解决问题;我以前能够依赖 gwt-dev 中的版本。

I am rather at my wits' end about this and considerably out of my comfort zone.

我对此相当不知所措,并且大大超出了我的舒适区。

回答by David Levesque

The order of the jars on the classpath matters. Did you try adding the Xerces 2.11 jar at the beginning of the classpath so it gets loaded first?

类路径上 jar 的顺序很重要。您是否尝试在类路径的开头添加 Xerces 2.11 jar 以便首先加载?

回答by Thomas Broyer

Don't fight with Maven: if things aren't used together, they should go in separate maven modules. In your case, JDom is (probably) used on the server-side, which doesn't need gwt-dev. So the solution is to split your project into several Maven modules: one for the client-side that depends on GWT, and one for the server-side that doesn't (or possibly on gwt-servlet if you use GWT-RPC, or on requestfactory-server if you use RequestFactory).

不要与 Maven 争吵:如果不一起使用,它们应该放在单独的 Maven 模块中。在您的情况下,JDom(可能)在服务器端使用,不需要 gwt-dev。因此,解决方案是将您的项目拆分为几个 Maven 模块:一个用于依赖 GWT 的客户端,另一个用于不依赖 GWT 的服务器端(如果您使用 GWT-RPC,则可能在 gwt-servlet 上,或者如果您使用 RequestFactory,则在 requestfactory-server 上)。

That said, even with a single project, if you do have gwt-dev in your classpath at runtime, then you got something wrong in your POM.

也就是说,即使是单个项目,如果您在运行时的类路径中确实有 gwt-dev,那么您的 POM 就会出现问题。

…unless you're reading your XML files at build-time?

...除非您在构建时读取 XML 文件?

回答by foal

In my case I resolved this issue by adding to bootstrap entities (Classpath tab in run configuration) two entries /xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar and /xerces/xercesImpl/2.11.0/xercesImpl-2.11.0.jar from my local maven repository

在我的情况下,我通过添加到引导实体(运行配置中的类路径选项卡)两个条目 /xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar 和 /xerces/xercesImpl/2.11 解决了这个问题.0/xercesImpl-2.11.0.jar 来自我的本地 Maven 存储库

回答by Zoltán

In my case this was resolved by deleting the xercesdirectory in my local sbt cache (local maven repository if you use maven), and rebuilding the project.

在我的情况下,这是通过删除xerces本地 sbt 缓存(本地 Maven 存储库,如果您使用 Maven)中的目录并重建项目来解决的。

回答by Svarozic

I had a same exception when I upgraded my project from GWT 2.7 to GWT 2.8. I have no idea why I had not this problem with GWT 2.7 (maybe different position of in .classpath file of Eclipse project could affect it).

当我将我的项目从 GWT 2.7 升级到 GWT 2.8 时,我遇到了同样的异常。我不知道为什么我在 GWT 2.7 中没有这个问题(可能 Eclipse 项目的 .classpath 文件中的不同位置可能会影响它)。

The reason for that exception was that before with such code like:

该异常的原因是之前有这样的代码:

DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
DocumentBuilder newDocumentBuilder = newInstance.newDocumentBuilder();
baseLayoutXmlDocument = newDocumentBuilder.parse( baseLayoutSvgInputStream );

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

the implementations from JDK package com.sun.org.apache.xerces.internal.jaxp was used, but after upgrade to GWT2.8 my app chose the xerces from gwt-dev.jar. I found the fix for that according to Javadoc and link hereto used system properties

使用了来自 JDK 包 com.sun.org.apache.xerces.internal.jaxp 的实现,但在升级到 GWT2.8 后,我的应用程序选择了来自 gwt-dev.jar 的 xerces。我根据 Javadoc 找到了修复程序,并在此处链接到使用的系统属性

-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

回答by JRSofty

This is a bit late, but after reading through the answers I did find one way to work around this problem. Instead of building your document factory with the normal DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();you could use the parameters in newInstanceto specifically choose. This way you don't have to add JVM parameters like Svarog's answer above, and you don't have to add or remove libraries. My solution is as follows:

这有点晚了,但在阅读了答案后,我确实找到了一种解决此问题的方法。DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();您可以使用其中的参数newInstance进行专门选择,而不是使用正常方式构建文档工厂。这样您就不必像上面 Svarog 的回答那样添加 JVM 参数,也不必添加或删除库。我的解决方案如下:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", this.getClass().getClassLoader());
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream("path/to/file.xml"));

回答by navin shah

The default JAXB implementation of WebLogic 12c causing the problem and you need to override the jaxb at the weblogic server startup. Add the below classpath to startWebLogic.cmd before echo CLASSPATH=%CLASSPATH%

WebLogic 12c 的默认 JAXB 实现导致了问题,您需要在 weblogic 服务器启动时覆盖 jaxb。在 echo CLASSPATH=%CLASSPATH% 之前将以下类路径添加到 startWebLogic.cmd

SET CLASSPATH=C:\Oracle\Middleware\Oracle_Home\wlserver\modules\databinding.override.jar;%CLASSPATH%

SET CLASSPATH=C:\Oracle\Middleware\Oracle_Home\wlserver\modules\databinding.override.jar;%CLASSPATH%

Tow JARS are added to support weblogic 12c.

添加了两个 JARS 以支持 weblogic 12c。

  1. jaxb-core.jar
  2. jaxb-impl.jar
  1. jaxb-core.jar
  2. jaxb-impl.jar