• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Difference between contextClassloader and Class.forname

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic