java XercesImpl 与 JavaSE 6 的内部 xerces 实现冲突。两者都需要......可以做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3110258/
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
XercesImpl in conflict with JavaSE 6's internal xerces implementation. Both are needed... what can be done?
提问by Yaneeve
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...)
使用 JDK 6 编译和运行代码是有效的(并且总是有......)
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
最近,我不得不将公司其他地方编写的另一个组件集成到我的代码中。该组件绝对需要包含在类路径中xercesImpl-2.8.1.jar
I absolutely require this 3rd party component, butnow running the code above no longer works and I get the following:
我绝对需要这个 3rd 方组件,但现在运行上面的代码不再有效,我得到以下信息:
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...
作为解决方案,我曾想过以某种方式将 xercesImpl-2.8.1.jar 屏蔽在它自己的类加载器中,但没有设法这样做,可能是由于缺乏类加载器知识,或者可能是因为它不是要走的路. 关于我的环境的另一件事,我的应用程序在 tomcat 5.5 和 6 上运行...
by the way while debugging I have noticed that when I run dom.getImplementation()
顺便说一下,在调试时我注意到当我运行时 dom.getImplementation()
- when adding the
xercesImpl-2.8.1.jarto 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
- 添加
xercesImpl-2.8.1.jar到类路径时,结果是org.apache.xerces.dom.DeferredDOMImplementationImpl@5f15c - 删除它时的结果是
com.sun.org.apache.xerces.internal.dom.DeferredDOMImplementationImpl@6c6ae3
[No surprise to you careful readers I suppose]
[我想你们细心的读者并不奇怪]
Any suggestions?
有什么建议?
采纳答案by Romain Hippeau
As per http://xml.apache.org/xalan-j/faq.html#faq-N100EF
根据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:
要使用更新版本的 Xalan-Java 并覆盖与 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
使用认可标准覆盖机制。将xalan.jar、serializer.jar、xercesImpl.jar和xml-apis.jar放在JRE的\lib\endorsed目录下,运行时软件的安装位置
回答by Gabriel
Instead of using:
而不是使用:
// Uses first classloader-available implementation found:
//import javax.xml.validation.SchemaFactory;
SchemaFactory schemaFactory= SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI);
Try using (Since Java 1.6):
尝试使用(自 Java 1.6 起):
// Uses org.apache.xerces.jaxp.validation.XMLSchemaFactory subclass
//of SchemaFactory as implementation:
//import javax.xml.validation.SchemaFactory;
SchemaFactory schemaFactory= SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI,
"org.apache.xerces.jaxp.validation.XMLSchemaFactory",
null);
See the related JavaDoc.
请参阅相关的 JavaDoc。
Or use META-INF/services engineering: article with examples
或者使用 META-INF/services engineering: article with examples
Hope it still helps somebody.
希望它仍然可以帮助某人。
Gabriel
加布里埃尔
回答by Vadzim
Note that it's possible to endorse libs without modifying jre by setting java.endorsed.dirssystem property.
请注意,通过设置java.endorsed.dirs系统属性,可以在不修改 jre 的情况下认可 libs 。
See what is the exact way to use Endorsed directory in jdk1.6.
回答by Yishai
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.
首先要尝试将 xerces jar 放在已背书的目录中。这将导致整个 JVM 一致地使用 Xerces。这可能会解决整个问题,除非我不知道 2.8.1 有什么特别之处。

