Wanderland

Wanderland
Friday Jul 18, 2008

java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

If your application suddenly stops working with the stack trace below:

Caused by: java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
       at org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:189) 

You might be wondering if different class loaders keep separate copies of xercesImpl.jar or if you need to provide xercesImpl.jar to your application to let it find org.apache.xerces.parsers.SAXParser, but it has worked before without adding xercesImpl.jar.

The answer: Glassfish does not have a xercesImpl.jar in its system classloader and you most likely will not need this jar.  The reason is when system property org.xml.sax.driver  is not specified and also no jars provide service API for org.xml.sax.driverorg.xml.sax.helpers.XMLReaderFactory.createXMLReader resolves to use a default XMLReader class which is SAX Parser specific. In SUN JDK5, the default class is com.sun.org.apache.xerces.internal.parsers.SAXParser.

Therefore, if another application changes the system property of  org.xml.sax.driver  and points it to org.apache.xerces.parsers.SAXParser, your application will fail with ClassNotFoundException.

How to resolve this issue without adding xercesImpl.jar? You might unset the system  property org.xml.sax.driver  in your application when you try to get an XMLReader, or you may use a fall-back mechanism like below:
 catch (Exception e) {
                  try {
                       // If unable to create an instance, let's try to use
                       // the XMLReader from JAXP
                       if (m_parserFactory == null) {
                           m_parserFactory = SAXParserFactory.newInstance();
                           m_parserFactory.setNamespaceAware(true);
                       }


                       reader = m_parserFactory.newSAXParser().getXMLReader();

Note that it is generally not a good idea to hard code a reference com.sun.org.apache.xerces.internal.parsers.SAXParser in your application, because the class might not be available when JDK upgrades or in other distributions of JDK .






Archives
Links
Referrers