Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XercesImpl in conflict with JavaSE 6's internal xerces implementation. Both are needed... what can be done?

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 is org.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?

like image 981
Yaneeve Avatar asked Jun 24 '10 13:06

Yaneeve


4 Answers

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):

// 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.

Or use META-INF/services engineering: article with examples

Hope it still helps somebody.

Gabriel

like image 58
Gabriel Avatar answered Oct 21 '22 08:10

Gabriel


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

like image 33
Romain Hippeau Avatar answered Oct 21 '22 07:10

Romain Hippeau


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.

like image 38
Yishai Avatar answered Oct 21 '22 08:10

Yishai


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.

like image 21
Vadzim Avatar answered Oct 21 '22 08:10

Vadzim