You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Alex Twisleton-Wykeham-Fiennes <al...@screenbase.com> on 2004/03/17 13:58:14 UTC

Classloader issues with DOM3 & JDK 1.4.2

All,

apologies if this is an FAQ, but I haven't been able to track down what the 
solution to this one is either in the docs or the mailing lists.

I have a small code snippet that tries to create a DOMImplementation like so:-

import org.apache.xerces.dom3.bootstrap.*;
import org.w3c.dom.*;

public class Test2
{
  public static void main(String[] args) 
    throws Exception
  {
    System.setProperty(DOMImplementationRegistry.PROPERTY,
		       "org.apache.xerces.dom.DOMImplementationSourceImpl");
    DOMImplementationRegistry registry 
      = DOMImplementationRegistry.newInstance();
    DOMImplementation impl 
      = (DOMImplementation) registry.getDOMImplementation("LS");
  }
}

Unfortunately this fails with the following:-
Exception in thread "main" java.lang.ClassCastException
        at org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown Source)
        at Test2.main(Test2.java:12)


I am using the following version of java:-
alex@nova:~/java/src> java -version
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

and am invoking the command utilising:-
java -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar Test2

If I run the application utilising the -verbose flag, and filter out all 
non-DOM related lines I get the following:-

alex@nova:~> java -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar -verbose Test2 | grep DOM
[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
[Loaded org.apache.xerces.dom3.DOMImplementationList]
[Loaded org.w3c.dom.DOMImplementationSource from /home/alex/java/lib/dom3-xml-apis.jar]
[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
[Loaded org.w3c.dom.DOMImplementationList from /home/alex/java/lib/dom3-xml-apis.jar]
[Loaded org.apache.xerces.dom3.DOMImplementationSource]
Exception in thread "main" java.lang.ClassCastException
        at org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown Source)
        at Test2.main(Test2.java:13)

and without the bootclasspath option gives me:-

alex@nova:~> java -verbose:class Test2 | grep DOM
[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
[Loaded org.apache.xerces.dom3.DOMImplementationList]
[Loaded org.w3c.dom.DOMImplementationSource]
[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
[Loaded org.w3c.dom.DOMImplementationList]
[Loaded org.apache.xerces.dom3.DOMImplementationSource]
Exception in thread "main" java.lang.ClassCastExceptionisnt instanceof DOMImplementationSource
        at org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown Source)
        at Test2.main(Test2.java:13)

which implies that the bootclasspath is forcing the bootstrap ClassLoader 
to load the DOMImplementationSource and the DOMImplementationList with all 
the other classes being loaded by some other ClassLoader (that doesn't 
specify which location they are being got from).  At least the org.w3c.dom 
classes are being got from the appropriate xerces jar which is a step in the 
right direction.

I then did a little bit of hacking around and added some debugging information 
into the DOMImplementationRegistry, to debug a bit what is going on, namely this:-

  public static void debug(Class c,
			   StringBuffer buf) 
  {
    buf.append("debug(")
      .append(c.getName())
      .append(")\n");
    buf.append("  .getSuperclass() [ ")
      .append(c.getSuperclass() == null
	      ? "null"
	      : c.getSuperclass().getName())
      .append(" ]\n");
    buf.append("  .getClassLoader() [ ")
      .append(c.getClassLoader())
      .append(" ]\n");
    buf.append("  .getInterfaces() [ ");
    Class[] classes = c.getInterfaces();
    for (int i = 0; i < classes.length; i++) {
      buf.append(classes[i].getName())
	.append(".getClassLoader()=")
	.append(classes[i].getClassLoader())
	.append(" ");
    }
    buf.append("]");
  }
  
  public static String debug(Class c) 
  {
    StringBuffer buf = new StringBuffer();
    debug(c,
	  buf);
    return buf.toString();
  }

and then added this into the DOMImplementationRegistry.newInstance() method to
give some feedback on what is being created like so:-

  public static DOMImplementationRegistry newInstance() 		
    throws ClassNotFoundException, InstantiationException, IllegalAccessException
  {
    Vector _sources = new Vector();      
    // fetch system property:
    String p = System.getProperty(PROPERTY);
    if (p != null) {
      StringTokenizer st = new StringTokenizer(p);
      while (st.hasMoreTokens()) {
	String sourceName = st.nextToken();
	// Use context class loader, falling back to Class.forName
	// if and only if this fails...
	Object source = getClass(sourceName).newInstance();
	System.out.println(debug(source.getClass()));
	System.out.println((source instanceof DOMImplementationSource)
		           ? "is instanceof DOMImplementationSource"
			   : "isnt instanceof DOMImplementationSource");
	_sources.add(source);
      }
    }
    return new DOMImplementationRegistry(_sources);
  }

Now my both my bootclasspath option and default options output the following:-

debug(org.apache.xerces.dom.DOMImplementationSourceImpl)
  .getSuperclass() [ java.lang.Object ]
  .getClassLoader() [ sun.misc.Launcher$AppClassLoader@136228 ]
  .getInterfaces() [ org.w3c.dom.DOMImplementationSource.getClassLoader()=null ]
isnt instanceof DOMImplementationSource
Exception in thread "main" java.lang.ClassCastException
        at org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown Source)
        at Test2.main(Test2.java:13)

The alarm bell for me was that although the DOMImplementationsSourceImpl had
been created with a sun.misc.Launcher$AppClassLoader ClassLoader, the
DOMImplementationSource interface had been instantiation by the bootstrap
ClassLoader.

Then when we say "source instanceof DOMImplementationSource", this will utilise
the default ClassLoader for the DOMImplementationRegistry (which is not the
bootstrap ClassLoader from the -verbose tests above) and create a seperate
instance of the DOMImplementationSource interface for the equivalent ClassLoader.
This is therefore not equal to the bootstrap provided instance of DOMImplementationSource
and therefore the result of "source instanceof DOMImplementationSource" is false.

This is all pretty new to me, but I am reasonably convinced that my steps in
getting to this point are relatively sane (and if they are not then would be
very grateful for anyones rapid enlightenment) and I am now in a position where
I do not know how to move forwards.  Is this a quirk of the JVM that I am using
and will it be necessary to downgrade to an earlier version or is there a more
complex underlying problem.

Many thanks for anyones help.

Alex

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Classloader issues with DOM3 & JDK 1.4.2

Posted by Alex Twisleton-Wykeham-Fiennes <al...@screenbase.com>.
On Wednesday 17 March 2004 14:19, Jacob Kjome wrote:
> Try adding dom3-xml-apis.jar to JAVA_HOME/jre/lib/endorsed.

this should (I believe) be identical to the -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar method in my invocation.  However, am at the point where I'll try anything now so we get (non-relevant lines clipped for sanity):-

alex@nova:~/java/src> java -verbose Test2
[Opened /opt/j2sdk1.4.2_04/jre/lib/endorsed/dom3-xml-apis.jar]
[Opened /opt/j2sdk1.4.2_04/jre/lib/rt.jar]
[Opened /opt/j2sdk1.4.2_04/jre/lib/sunrsasign.jar]
[Opened /opt/j2sdk1.4.2_04/jre/lib/jsse.jar]
[Opened /opt/j2sdk1.4.2_04/jre/lib/jce.jar]
[Opened /opt/j2sdk1.4.2_04/jre/lib/charsets.jar]
...
[Loaded Test2]
...
[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
[Loaded org.apache.xerces.dom3.DOMImplementationList]
...
[Loaded org.w3c.dom.DOMImplementationSource from /opt/j2sdk1.4.2_04/jre/lib/endorsed/dom3-xml-apis.jar]
[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
[Loaded org.w3c.dom.DOMImplementationList from /opt/j2sdk1.4.2_04/jre/lib/endorsed/dom3-xml-apis.jar]
debug(org.apache.xerces.dom.DOMImplementationSourceImpl)
  .getSuperclass() [ java.lang.Object ]
  .getClassLoader() [ sun.misc.Launcher$AppClassLoader@136228 ]
  .getInterfaces() [ org.w3c.dom.DOMImplementationSource.getClassLoader()=null ]
[Loaded org.apache.xerces.dom3.DOMImplementationSource]
isnt instanceof DOMImplementationSource
Exception in thread "main" java.lang.ClassCastException
...
        at org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown Source)
        at Test2.main(Test2.java:13)
...

So, the good news is that it is looking in the correct jar in the endorsed directory (and it never opens any other jars other than the ones that I've listed.  
The bad news is that there are still two different classloaders doing the loading, with the bootstrap classloader only responsible for the DOMImplementationSource and the DOMImplementationList.

Therefore we are no further down the path to enlightenment.

<snip>

Alex

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Classloader issues with DOM3 & JDK 1.4.2

Posted by Jacob Kjome <ho...@visi.com>.
Try adding dom3-xml-apis.jar to JAVA_HOME/jre/lib/endorsed.

Jake


At 12:58 PM 3/17/2004 +0000, you wrote:
>All,
>
>apologies if this is an FAQ, but I haven't been able to track down what the
>solution to this one is either in the docs or the mailing lists.
>
>I have a small code snippet that tries to create a DOMImplementation like so:-
>
>import org.apache.xerces.dom3.bootstrap.*;
>import org.w3c.dom.*;
>
>public class Test2
>{
>   public static void main(String[] args)
>     throws Exception
>   {
>     System.setProperty(DOMImplementationRegistry.PROPERTY,
>                        "org.apache.xerces.dom.DOMImplementationSourceImpl");
>     DOMImplementationRegistry registry
>       = DOMImplementationRegistry.newInstance();
>     DOMImplementation impl
>       = (DOMImplementation) registry.getDOMImplementation("LS");
>   }
>}
>
>Unfortunately this fails with the following:-
>Exception in thread "main" java.lang.ClassCastException
>         at 
> org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown 
> Source)
>         at Test2.main(Test2.java:12)
>
>
>I am using the following version of java:-
>alex@nova:~/java/src> java -version
>java version "1.4.2_04"
>Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
>Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
>
>and am invoking the command utilising:-
>java -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar Test2
>
>If I run the application utilising the -verbose flag, and filter out all
>non-DOM related lines I get the following:-
>
>alex@nova:~> java -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar 
>-verbose Test2 | grep DOM
>[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
>[Loaded org.apache.xerces.dom3.DOMImplementationList]
>[Loaded org.w3c.dom.DOMImplementationSource from 
>/home/alex/java/lib/dom3-xml-apis.jar]
>[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
>[Loaded org.w3c.dom.DOMImplementationList from 
>/home/alex/java/lib/dom3-xml-apis.jar]
>[Loaded org.apache.xerces.dom3.DOMImplementationSource]
>Exception in thread "main" java.lang.ClassCastException
>         at 
> org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown 
> Source)
>         at Test2.main(Test2.java:13)
>
>and without the bootclasspath option gives me:-
>
>alex@nova:~> java -verbose:class Test2 | grep DOM
>[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
>[Loaded org.apache.xerces.dom3.DOMImplementationList]
>[Loaded org.w3c.dom.DOMImplementationSource]
>[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
>[Loaded org.w3c.dom.DOMImplementationList]
>[Loaded org.apache.xerces.dom3.DOMImplementationSource]
>Exception in thread "main" java.lang.ClassCastExceptionisnt instanceof 
>DOMImplementationSource
>         at 
> org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown 
> Source)
>         at Test2.main(Test2.java:13)
>
>which implies that the bootclasspath is forcing the bootstrap ClassLoader
>to load the DOMImplementationSource and the DOMImplementationList with all
>the other classes being loaded by some other ClassLoader (that doesn't
>specify which location they are being got from).  At least the org.w3c.dom
>classes are being got from the appropriate xerces jar which is a step in the
>right direction.
>
>I then did a little bit of hacking around and added some debugging 
>information
>into the DOMImplementationRegistry, to debug a bit what is going on, 
>namely this:-
>
>   public static void debug(Class c,
>                            StringBuffer buf)
>   {
>     buf.append("debug(")
>       .append(c.getName())
>       .append(")\n");
>     buf.append("  .getSuperclass() [ ")
>       .append(c.getSuperclass() == null
>               ? "null"
>               : c.getSuperclass().getName())
>       .append(" ]\n");
>     buf.append("  .getClassLoader() [ ")
>       .append(c.getClassLoader())
>       .append(" ]\n");
>     buf.append("  .getInterfaces() [ ");
>     Class[] classes = c.getInterfaces();
>     for (int i = 0; i < classes.length; i++) {
>       buf.append(classes[i].getName())
>         .append(".getClassLoader()=")
>         .append(classes[i].getClassLoader())
>         .append(" ");
>     }
>     buf.append("]");
>   }
>
>   public static String debug(Class c)
>   {
>     StringBuffer buf = new StringBuffer();
>     debug(c,
>           buf);
>     return buf.toString();
>   }
>
>and then added this into the DOMImplementationRegistry.newInstance() method to
>give some feedback on what is being created like so:-
>
>   public static DOMImplementationRegistry newInstance()
>     throws ClassNotFoundException, InstantiationException, 
> IllegalAccessException
>   {
>     Vector _sources = new Vector();
>     // fetch system property:
>     String p = System.getProperty(PROPERTY);
>     if (p != null) {
>       StringTokenizer st = new StringTokenizer(p);
>       while (st.hasMoreTokens()) {
>         String sourceName = st.nextToken();
>         // Use context class loader, falling back to Class.forName
>         // if and only if this fails...
>         Object source = getClass(sourceName).newInstance();
>         System.out.println(debug(source.getClass()));
>         System.out.println((source instanceof DOMImplementationSource)
>                            ? "is instanceof DOMImplementationSource"
>                            : "isnt instanceof DOMImplementationSource");
>         _sources.add(source);
>       }
>     }
>     return new DOMImplementationRegistry(_sources);
>   }
>
>Now my both my bootclasspath option and default options output the following:-
>
>debug(org.apache.xerces.dom.DOMImplementationSourceImpl)
>   .getSuperclass() [ java.lang.Object ]
>   .getClassLoader() [ sun.misc.Launcher$AppClassLoader@136228 ]
>   .getInterfaces() [ 
> org.w3c.dom.DOMImplementationSource.getClassLoader()=null ]
>isnt instanceof DOMImplementationSource
>Exception in thread "main" java.lang.ClassCastException
>         at 
> org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown 
> Source)
>         at Test2.main(Test2.java:13)
>
>The alarm bell for me was that although the DOMImplementationsSourceImpl had
>been created with a sun.misc.Launcher$AppClassLoader ClassLoader, the
>DOMImplementationSource interface had been instantiation by the bootstrap
>ClassLoader.
>
>Then when we say "source instanceof DOMImplementationSource", this will 
>utilise
>the default ClassLoader for the DOMImplementationRegistry (which is not the
>bootstrap ClassLoader from the -verbose tests above) and create a seperate
>instance of the DOMImplementationSource interface for the equivalent 
>ClassLoader.
>This is therefore not equal to the bootstrap provided instance of 
>DOMImplementationSource
>and therefore the result of "source instanceof DOMImplementationSource" is 
>false.
>
>This is all pretty new to me, but I am reasonably convinced that my steps in
>getting to this point are relatively sane (and if they are not then would be
>very grateful for anyones rapid enlightenment) and I am now in a position 
>where
>I do not know how to move forwards.  Is this a quirk of the JVM that I am 
>using
>and will it be necessary to downgrade to an earlier version or is there a more
>complex underlying problem.
>
>Many thanks for anyones help.
>
>Alex
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org