aspose file tools*
The moose likes Java in General and the fly likes Difference between contextClassloader and Class.forname Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between contextClassloader and Class.forname" Watch "Difference between contextClassloader and Class.forname" New topic
Author

Difference between contextClassloader and Class.forname

Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Hi,
can anyone tell me the differnece between
thread.currentThread().getContextClassloader().loadClass("<class-name>")
and
class.forname("<class-name>")?

I am really confused between this two. I went through the javadoc also. I am also not aware of what does thread.currentthread.contextClassLoader() will return? Doen't the contextclassloader and class's classLoader are same?


Chop your own wood, and it will warm you twice. - Henry Ford
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Kartik Patel:
Doen't the contextclassloader and class's classLoader are same?


No, certainly not.

The current class's ClassLoader is whatever loaded that class and is fixed.

The thread context ClassLoader is under your control; you can set it. If you haven't set it yourself, it defaults to the thread context ClassLoader of the thread that started the current thread. If the current thread is the main thread, the default context ClassLoader is the system ClassLoader (the one that works with the classpath).

In applications with multiple ClassLoaders, I tend to find that Class.forName() usually does the wrong thing. I tend to use the thread context ClassLoader, which I have set appropriately.
[ July 28, 2006: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Thanks Peter for your reply.

Just to add to this I now have the clear concept of the difference between these two.

If anyone is still confuse, he/she can go through this link.
This topic extensively explained and clear many other doubts of classloading

Understanding Class.forName()
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Please DontWakeTheZombies. I am not sure that I agree with what you have written, either. But please don't continue the discussion in this thread. If you want to discuss further, please start a new thread.

(Looks like the zombie-waker has now deleted his post)
[ February 29, 2008: Message edited by: Peter Chase ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between contextClassloader and Class.forname
 
Similar Threads
Class Loader irritates :(
role name in servelt element
Navigation issue
web.xml tags
404 in using acegi security with Spring