Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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.

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.

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

like image 997
ejoftheweb Avatar asked Jul 17 '14 13:07

ejoftheweb


3 Answers

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

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 here to used system properties

-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
like image 88
Svarozic Avatar answered Nov 05 '22 08:11

Svarozic


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

like image 29
foal Avatar answered Nov 05 '22 06:11

foal


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 newInstance to 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("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"));
like image 2
JRSofty Avatar answered Nov 05 '22 07:11

JRSofty