I have an application that explicitly manages ClassLoaders and is intended to work with a wide range of versions of Xerces (including even some versions 1.*, which is why I'm still using the old API). I have a class named LocatedElementImpl; its one method that uses Xerces has the following outline:
(1) Thread.currentThread().setContextClassLoader(LocatedElementImpl.class.getClassLoader());
(2) create a new org.apache.xerces.parsers.DOMParser();
(3) set the error handler, some features, and some properties; and
(4) call parser.parse, inside a try-catch that handles a RuntimeException by (a) printing the exception and (b) fetching and printing the now-current context class loader.
When using Xerces 2.*, LocatedElementImpl's ClassLoader (which is a URLClassLoader) also loads from xercesImpl.jar (among other things), while its parent (which is the application ClassLoader) loads from xmlParserAPIs.jar (among other things); when using Xerces 1.*, xerces.jar is available to both ClassLoaders (which means the application ClassLoader, to which LocatedElementImpl's ClassLoader delegates first, will load all Xerces classes).
This code has been successfully compiled and tested with Xerces versions 1.4.4, 2.0.1, 2.0.2, 2.1.0, and 2.2.1 (2.2.0 is a known bad guy for (at least) other reasons). However, when compiled and run with Xerces 2.3.0, the call on parse throws a ClassCastException. Below is the output from my catch phrase:
java.lang.ClassCastException: org.apache.xerces.impl.dv.xs.ListDV$ListData
at org.apache.xerces.impl.xs.XMLSchemaLoader.processExternalHints(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.reset(Unknown Source)
at org.apache.xerces.parsers.BasicParserConfiguration.reset(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.reset(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at com.ibm.wsst.common.LocatedElementImpl.readXml(LocatedElementImpl.java:539)
at com.ibm.wsst.common.LocatedElementImpl.<clinit>(LocatedElementImpl.java:68)
at com.ibm.wsst.engine.Engine.run(Engine.java:537)
at java.lang.Thread.run(Thread.java:512)
No linked exception.
ContextClassLoader is:
ClassLoader [EMAIL PROTECTED]: has 4 URLS:
file:/C:/mspreitz/build/wsst/lib/wsst.jar
file:/c:/xml-axis-10/lib/log4j-1.2.4.jar
file:/c:/xerces-2_3_0/xerces.jar
file:/c:/xerces-2_3_0/xercesImpl.jar
Has parent:
ClassLoader [EMAIL PROTECTED]: has 4 URLS:
file:/C:/mspreitz/build/wsst/
file:/C:/mspreitz/build/wsst/lib/boot.jar
file:/C:/xerces-2_3_0/xerces.jar
file:/C:/xerces-2_3_0/xmlParserAPIs.jar
Has parent:
ClassLoader [EMAIL PROTECTED]: has 13 URLS:
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/comm.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/ibmjcaprovider.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/ibmjcefw.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/ibmjceprovider.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/ibmpkcs.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/ibmpkcs11.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/indicim.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/jaas.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/jaas_activelm.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/jaas_lm.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/JawBridge.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/local_policy.jar
file:/C:/Program%20Files/IBM/Java131/jre/lib/ext/US_export_policy.jar
No parent.
(No, there is no file named "c:/xerces-2_3_0/xerces.jar" --- but that's OK, the needed classes are found in the JARs that DO exist; these URL sets are computed by stupid scripts that are intended to work with both Xerces 1 and Xerces 2.)
What's going wrong here, and what can be done about it?
Thanks,
Mike
- java.lang.ClassCastException: org.apache.xerces.impl.dv.xs.... Mike Spreitzer
- Re: java.lang.ClassCastException: org.apache.xerces.im... Mike Spreitzer
- Re: java.lang.ClassCastException: org.apache.xerces.im... Neil Graham
- Re: java.lang.ClassCastException: org.apache.xerce... Mike Spreitzer