By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Help: ClassLoader and Casting??

P: n/a
I need to load a different version of a class than what is being used
by the overall system. My test code works fine for loading the class,
but fails when I try to cast the Object. For example, the following
throws a java.lang.NoClassDefFoundError when trying to cast.

public class URLTester
{
public static void main( String args[] ) throws Exception
{
// Create an array of URLs
// From directory name specified as args[0]
URL urlList[] = {new File( args[ 0 ] ).toURL()};

// Create a URLClassLoader
ClassLoader loader = new URLClassLoader( urlList );
// Thread.currentThread().setContextClassLoader( loader );

// Load class from class loader
// Use args[1] as class name
Class c = loader.loadClass( args[ 1 ] );

// Create an instance of the class just loaded
Object obj = c.newInstance();

// This fails with a NoClassDefFoundError
Tester tester = (Tester)obj;
}
}

My hunch is that the system ClassLoader is doing the casting. I've
tried, with no success, to add
Thread.currentThread().setContextClassLoader( loader ) to change the
thread's ClassLoader.

Any help would be greatly appreciated!

-- John
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.