[jdom-interest] SAXBuilder issues - class cast exception

Krishnan Anantheswaran krishnan_anantheswaran at yahoo.com
Wed Jun 27 14:01:12 PDT 2001


Jason,

Thanks for the pointer. The issue seems to be a
classloader issue. Ant has its own classloader that it
uses to create dynamic classpaths for different tasks
etc. However (some of) the xerces.jar classes are
loaded by the NetBeans IDE before it uses Ant so this
uses the system classloader. 

What happens at the end of it all is that the
org.xml.sax.XMLReader class known to the SAXBuilder is
loaded thru the Ant classloader and the returned
XMLReader interface has been created by the JDK
classloader. Therefore, this results in a class cast
exception.

I *still* can't figure out how to work around this
issue. If somebody knows please let me know.

Code and output follows.

Regards,
Krishnan

// added the following methods to SAXBuilder

private void printClassTruth(String text, Class clazz)
{

  System.err.println(text + ":");
  System.err.println("\tClass name   : " + 
              clazz.getName());
  System.err.println("\tClass loader : " +
     clazz.getClassLoader().getClass().getName());

  ClassLoader parent = 
      clazz.getClassLoader().getParent();

  if (parent != null)
    System.err.println("\tParent class loader : " +
                parent.getClass().getName());
  else
    System.err.println("\tParent class loader :
null");
}

private void printClassTruth(String text, 
        Class[] clazzes) {

  for (int i=0; i<clazzes.length; i++)
      printClassTruth(text + " " + i,clazzes[i]);
}

// Changed the line
// parser = (XMLReader) getXMLReader.invoke
//                             (jaxpParser,null)
// to

Object o = getXMLReader.invoke(jaxpParser, null);
printClassTruth("SAXBuilder class",getClass());
printClassTruth("getXMLReader return class",
             o.getClass());
printClassTruth("my org.xml.sax.XMLReader",
                org.xml.sax.XMLReader.class);
printClassTruth("returned interfaces",
            o.getClass().getInterfaces());

parser = (XMLReader) o;

/////////////////////////////////////
// Output is
/////////////////////////////////////

SAXBuilder class:
        Class name   : org.jdom.input.SAXBuilder
        Class loader :
org.apache.tools.ant.AntClassLoader
        Parent class loader :
sun.misc.Launcher$AppClassLoader

getXMLReader return class:
        Class name   :
org.apache.xerces.parsers.SAXParser
        Class loader :
sun.misc.Launcher$AppClassLoader
        Parent class loader :
sun.misc.Launcher$ExtClassLoader

my org.xml.sax.XMLReader:
        Class name   : org.xml.sax.XMLReader
        Class loader :
org.apache.tools.ant.AntClassLoader
        Parent class loader :
sun.misc.Launcher$AppClassLoader

returned interfaces 0:
        Class name   :
org.apache.xerces.framework.XMLDocumentHandler
        Class loader :
sun.misc.Launcher$AppClassLoader
        Parent class loader :
sun.misc.Launcher$ExtClassLoader

returned interfaces 1:
        Class name   :
org.apache.xerces.framework.XMLDocumentHandler$DTDHandler
        Class loader :
sun.misc.Launcher$AppClassLoader
        Parent class loader :
sun.misc.Launcher$ExtClassLoader

returned interfaces 2:
        Class name   : org.xml.sax.Parser
        Class loader :
sun.misc.Launcher$AppClassLoader
        Parent class loader :
sun.misc.Launcher$ExtClassLoader

returned interfaces 3:
        Class name   : org.xml.sax.XMLReader
        Class loader :
sun.misc.Launcher$AppClassLoader
        Parent class loader :
sun.misc.Launcher$ExtClassLoader



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/



More information about the jdom-interest mailing list