I have installed my ClassLoader as the System classloader (using -Djava.system.class.loader). My ClassLoader extends URLClassLoader. In the constructor (the only one called by the bootclasspath loader is: YourClassLoader(ClassLoader parent) ) I call super(URL[] urls, ClassLoader parent) by passing it like so: MyClassLoader(ClassLoader parent) { super( classPathURLS, parent ); }
When I do the above, I get a class cast exception when I try to cast a class that a class loader instance (in the program below) created to an interface that my class loader defined. public Doable load() { URLClassLoader uc = new URLClassLoader( someURLNotOnClassPath ); Class c = uc.loadClass( "com.something.Test" );
/* * This cast only throws an exception if my system classloader * passes "parent" to the constructor of "super." If I instead * do the following: "super( classPathURLS, null );" then I * never get a ClassCastException. Does anyone know why this is? */ return ( Doable ) c.newInstance(); }
Just to give some more information on the problem: in both cases (passing "parent" to "super()" and passing "null" to "super()"), the ClassLoader of the "Doable" class is always MyClassLoader. Also, I have logging in every method called (when an attempt is made to load or find a class) and it is called for Doable only once - whereupon I define and load the class.
I'm sure I am doing something wrong, but I'm not sure what. Does anyone have any idea what it might be?