On Fri, 16 Feb 2001, you wrote:
> This, however, seems to obviate the purpose of the setContextClassLoader
> method.  What exactly is it good for?

A question I would dearly like to know the answer to...

Maybe Class.forName and that sort of method use it?

> Do you think this code would work if modified as below (I haven't tested
> this)?

No, I tried this too.

> Here, the current thread's (i.e.,the main thread)  classloader is set to be
> the URLClassLoader "cl"...
> but cl's parent is the System classloader, so it should be resolving classes
> too.
> Also, since DCTest is loaded using cl, any classes loaded from code within
> DCTest MUST use cl to resolve classes, correct?

No, see below.

> Maybe the only way to solve this problem is to use a "bootstrap" class
> loader with the system classloader as its parent, as in the code below?

Yes, this seems to be the way to go.  But you can't do it like this:

> public class mainClass{
> 
>       public static void main( String args[] ){
> // Commandline argument is url to load classes from
>               java.net.URL urls[] = new java.net.URL[1];
>               urls[0] = new java.net.URL( args[0] );
>               ClassLoader cl = new
> ava.net.URLClassLoader( urls,ClassLoader.getSystemClassLoader );
>               Thread.getCurrentThread().setContextClassLoader( cl );

                // Here it is loaded once, by cl

>               cl.loadClass( "DCTest");

                // And here it is loaded again, by the system class loader.

>               Thread t = new Thread( new DCTest() );
>               t.start();
>               while( t.isAlive() );
>       }
> 
> 
> }

[snip the rest of the code]

This doesn't work because, as I've indicated, it causes the class DCTest to be
loaded twice, once by cl in the loadClass call, and again by the system
classloader (which will throw an exception which we are getting painfully used
to...)

It would seem the only way is:

ClassLoader cl = new URLClassLoader(...);
Class c = cl.loadClass( "DCTest" );
Runnable r = (Runnable)c.newInstance();
Thread t = new Thread( r );
r.run();

which is a lot nastier than direct referencing, esp. if you have lots of such
objects to instantiate, but it's the only way...

Tom


--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
List Help?:          [EMAIL PROTECTED]

Reply via email to