using Thread.currentThread().getContextClassLoader() rather than class.forName()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

using Thread.currentThread().getContextClassLoader() rather than class.forName()

jian chen
Hi, All,

I am having an issue with Class.forName(...) and want to get some advice.

I have an embedded jetty servlet engine and the web app is a war file.
I started up the web app using a java program. It worked fine.

I wanted to do a refactoring and took a method that uses
Class.forName(...) out of the war file and into a library file. Once I
did that, I started up jetty by
java -cp .;mylib.jar portal.war

Then, the Class.forName() could not load the class needed.

I think this is because Jetty uses its own class loader for portal.war
file unpacking.

If I change the Class.forName() to
Thread.currentThread().getContextClassLoader().loadClass(clazz)
it worked fine.

So, my question is, is it always safe to use
Thread.currentThread().getContextClassLoader().loadClass(clazz)
instead of Class.forName() ?

Thanks for any suggestions!

Jian


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
<a href="http://ads.osdn.com/?ad_ids93&alloc_id281&op=click">http://ads.osdn.com/?ad_ids93&alloc_id281&op=click
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss
Reply | Threaded
Open this post in threaded view
|

Re: using Thread.currentThread().getContextClassLoader() rather than class.forName()

tmjee-3
In my opinion, I think it is always better to use Thread's context class
loader, because Class.forName() would use the current class loader that load
the class which might be in a higher level of hierarchy that the thread that
was spawn to actually run on that class during request.

Hope this helps

regards
tmjee

----- Original Message -----
From: "jian chen" <[hidden email]>
To: <[hidden email]>
Sent: Friday, May 13, 2005 7:49 AM
Subject: [jetty-discuss] using
Thread.currentThread().getContextClassLoader() rather than class.forName()


Hi, All,

I am having an issue with Class.forName(...) and want to get some advice.

I have an embedded jetty servlet engine and the web app is a war file.
I started up the web app using a java program. It worked fine.

I wanted to do a refactoring and took a method that uses
Class.forName(...) out of the war file and into a library file. Once I
did that, I started up jetty by
java -cp .;mylib.jar portal.war

Then, the Class.forName() could not load the class needed.

I think this is because Jetty uses its own class loader for portal.war
file unpacking.

If I change the Class.forName() to
Thread.currentThread().getContextClassLoader().loadClass(clazz)
it worked fine.

So, my question is, is it always safe to use
Thread.currentThread().getContextClassLoader().loadClass(clazz)
instead of Class.forName() ?

Thanks for any suggestions!

Jian


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
<a href="http://ads.osdn.com/?ad_ids93&alloc_id281&op=ick">http://ads.osdn.com/?ad_ids93&alloc_id281&op=ick
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7393&alloc_id=16281&op=click
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss
Reply | Threaded
Open this post in threaded view
|

Re: using Thread.currentThread().getContextClassLoader() rather than class.forName()

jian chen
Hi,  Tmjee,

Thanks a lot for your info. --Jian

On 5/12/05, tmjee <[hidden email]> wrote:

> In my opinion, I think it is always better to use Thread's context class
> loader, because Class.forName() would use the current class loader that load
> the class which might be in a higher level of hierarchy that the thread that
> was spawn to actually run on that class during request.
>
> Hope this helps
>
> regards
> tmjee
>
> ----- Original Message -----
> From: "jian chen" <[hidden email]>
> To: <[hidden email]>
> Sent: Friday, May 13, 2005 7:49 AM
> Subject: [jetty-discuss] using
> Thread.currentThread().getContextClassLoader() rather than class.forName()
>
>
> Hi, All,
>
> I am having an issue with Class.forName(...) and want to get some advice.
>
> I have an embedded jetty servlet engine and the web app is a war file.
> I started up the web app using a java program. It worked fine.
>
> I wanted to do a refactoring and took a method that uses
> Class.forName(...) out of the war file and into a library file. Once I
> did that, I started up jetty by
> java -cp .;mylib.jar portal.war
>
> Then, the Class.forName() could not load the class needed.
>
> I think this is because Jetty uses its own class loader for portal.war
> file unpacking.
>
> If I change the Class.forName() to
> Thread.currentThread().getContextClassLoader().loadClass(clazz)
> it worked fine.
>
> So, my question is, is it always safe to use
> Thread.currentThread().getContextClassLoader().loadClass(clazz)
> instead of Class.forName() ?
>
> Thanks for any suggestions!
>
> Jian
>


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
<a href="http://ads.osdn.com/?ad_ids93&alloc_id281&op=click">http://ads.osdn.com/?ad_ids93&alloc_id281&op=click
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss