XercesImpl 与 JavaSE 6 的内部 xerces 实现冲突。两者都需要......可以做什么?
我确信我不是第一个遇到这种冲突的人。
我继承的代码执行以下操作:
org.w3c.dom.Document dom; // declaration
javax.xml.validation.Schema schema; // declaration
...
...
...
javax.xml.validation.Validator validator = schema.newValidator();
validator.validate(new DOMSource(dom));
其中 ...
代表看似不重要/不相关的代码
使用 JDK 6 编译和运行代码可以工作(并且总是有...)
最近我有过将公司其他地方编写的另一个组件集成到我的代码中。该组件绝对需要包含在 xercesImpl-2.8.1.jar
的类路径中,
我绝对需要这个第 3 方组件,但是现在运行上面的代码不再有效,我得到以下内容:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Root'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Validator.java:127)
作为一种解决方案,我可能想以某种方式将 xercesImpl-2.8.1.jar 屏蔽在它自己的类加载器中,但没有成功,可能是由于缺乏类加载器知识,或者可能是因为它不是要走的路。关于我的环境的另一件事是,我的应用程序在 tomcat 5.5 和 6 上运行...
顺便说一下,在调试时,我注意到当我运行 dom.getImplementation()
- 时添加
xercesImpl-2.8.1.jar
到 类路径结果是org.apache.xerces.dom.DeferredDOMImplementationImpl@5f15c
- 删除它时,结果是
com.sun.org.apache.xerces.internal.dom.DeferredDOMImplementationImpl@6c6ae3
[我想,对于细心的读者来说,这并不奇怪]
有什么建议吗?
I am sure that I am not the first to encounter this conflict.
The code that I have inherited does the following:
org.w3c.dom.Document dom; // declaration
javax.xml.validation.Schema schema; // declaration
...
...
...
javax.xml.validation.Validator validator = schema.newValidator();
validator.validate(new DOMSource(dom));
where the ...
stands for seemingly unimportant/irrelevant code
Compiling and running the code with JDK 6 works (and always had...)
Recently I have had to integrate into my code another component written elsewhere in the company. That component absolutely requires the inclusion in the classpath of xercesImpl-2.8.1.jar
I absolutely require this 3rd party component, but now running the code above no longer works and I get the following:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Root'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Validator.java:127)
As a solution, I have thought perhaps somehow to shield the xercesImpl-2.8.1.jar in a classloader of its own, but have not managed to do so, perhaps due to lack of classloader knowledge or perhaps because its not the way to go. One more thing about my environment, my app runs on tomcat 5.5 and 6...
by the way while debugging I have noticed that when I run dom.getImplementation()
- when adding the
xercesImpl-2.8.1.jar
to the
classpath the result isorg.apache.xerces.dom.DeferredDOMImplementationImpl@5f15c
- when removing it the result is
com.sun.org.apache.xerces.internal.dom.DeferredDOMImplementationImpl@6c6ae3
[No surprise to you careful readers I suppose]
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而不是使用:
尝试使用(自 Java 1.6 起):
请参阅相关的 JavaDoc。
或者使用 META-INF/services 工程:带有示例的文章
希望它仍然对某人有所帮助。
加布里埃尔
Instead of using:
Try using (Since Java 1.6):
See the related JavaDoc.
Or use META-INF/services engineering: article with examples
Hope it still helps somebody.
Gabriel
根据 http://xml.apache.org/xalan-j/faq .html#faq-N100EF
要使用较新版本的 Xalan-Java 并覆盖与 JDK 一起打包的版本:
使用认可标准覆盖机制。将 xalan.jar、serializer.jar、xercesImpl.jar 和 xml-apis.jar 放在 JRE 的 \lib\endorsed 目录中,该目录是运行时软件的安装位置
As per http://xml.apache.org/xalan-j/faq.html#faq-N100EF
To use a newer version of Xalan-Java and override the one packaged with the JDK:
use the Endorsed Standards Override Mechanism. Place the xalan.jar, serializer.jar, xercesImpl.jar and xml-apis.jar in the \lib\endorsed directory of the JRE, where is where the runtime software is installed
首先要尝试的是将 xerces jar 放入认可目录中。这将导致整个 JVM 一致地使用 Xerces。这可能会解决整个问题,除非 2.8.1 有一些我不知道的特殊情况。
The first thing to try is to put the xerces jar in the endorsed directory. That will cause the whole JVM to use Xerces consistently. That may solve the whole problem right there, unless there is something special about 2.8.1 I don't know about.
请注意,可以通过设置 java.util.lib 来认可库而不修改 jre。 Endorsed.dirs 系统属性。
请参阅确切的使用方法是什么jdk1.6中认可的目录。
Note that it's possible to endorse libs without modifying jre by setting java.endorsed.dirs system property.
See what is the exact way to use Endorsed directory in jdk1.6.