The Artima Developer Community
Sponsored Link

Java Answers Forum
Why getting ClassCastException?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Robert Paris

Posts: 1
Nickname: rpjava
Registered: Feb, 2004

Why getting ClassCastException? Posted: Feb 17, 2004 10:40 AM
Reply to this message Reply
Advertisement
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?

Topic: Drawing Method Problem Previous Topic   Next Topic Topic: parameters getting dropped from request object

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use