File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Difference between getClass.getclassloader and tThread.getContextClassLoader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between getClass.getclassloader and tThread.getContextClassLoader" Watch "Difference between getClass.getclassloader and tThread.getContextClassLoader" New topic
Author

Difference between getClass.getclassloader and tThread.getContextClassLoader

Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

Hi,

I wanted to know the difference between Thread.currentThread().getContextClassLoader() and getClass().getClassLoader()

This is what i got in the api's



Class.getClassLoader() returns the ClassLoader that loaded the class it
is invoked on.

Thread.getContextClassLoader() returns the ClassLoader set as the
context ClassLoader for the Thread it is invoked on



This is what i got after googling up this topic

Class.getClassLoader's classloader can be
different from the ClassLoader that loaded the Thread class itself if
the Thread's setContextClassLoader(ClassLoader) method has been invoked.



Now my question

If i have not called Thread's setContextClassLoader(ClassLoader) can the two methods return different classloaders in a j2ee environment ?

The reason for my asking is we use OC4J at work ... in certain rare situations.

Thread.currentThread().getContextClassLoader().getResourceAsStream(String name) does not seem to return the stream

However getClass().getClassLoader().getResourceAsStream(String name)
seems to get the job done.

Given the above info i need an explanation with scenario example preferably as to why i am being returned two different class loaders.


Thanks in advance.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
As I'm not a Barender, I can't close this threa, but please do not post the same question in multiple forums !!
Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

posted here by mistake sorry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between getClass.getclassloader and tThread.getContextClassLoader