-
Class.forName() vs getContextClassLoader().loadClass() (4 messages)
- Posted by: D Liang
- Posted on: September 03 2008 22:18 EDT
Hi all, I've stumbled by these two methods recently: Class.forName(className); Thread.currentThread().getContextClassLoader().loadClass(className); I want to know the reason why most programmer is cheering for the latter method? Have been reading a lot about classloading articles but have not got a conclusion yet?Threaded Messages (4)
- because of ... by Anton Chistyakov on September 04 2008 03:37 EDT
- Class.forName() vs getContextClassLoader().loadClass() by steven baker on May 03 2011 00:46 EDT
- Class.forName() vs getContextClassLoader().loadClass() by Ram Bandarupalli on May 10 2011 06:37 EDT
-
because of ...[ Go to top ]
- Posted by: Anton Chistyakov
- Posted on: September 04 2008 03:37 EDT
- in response to D Liang
They are not the equivalent. :-) they use different class loaders. Class.forName(String) == Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class. Thread.currentThread().getContextClassLoader().loadClass(className); will load class using context class loader. -
thanks![ Go to top ]
- Posted by: D Liang
- Posted on: September 10 2008 05:48 EDT
- in response to Anton Chistyakov
thanks a lot!I have found some explains online and i have got them already!thanks anyway! -
Class.forName() vs getContextClassLoader().loadClass()[ Go to top ]
- Posted by: steven baker
- Posted on: May 03 2011 00:46 EDT
- in response to D Liang
the biggest difference ive seen is the later can load from outside of the same jar. so if the class your using to load is in a different jar to the class you want, then it would otherwise fail with class.forname. i suggest using the context version as default since you never know where your code will end up. this is particually important when working with application containers
-
Class.forName() vs getContextClassLoader().loadClass()[ Go to top ]
- Posted by: Ram Bandarupalli
- Posted on: May 10 2011 06:37 EDT
- in response to steven baker
the biggest difference ive seen is the later can load from outside of the same jar. so if the class your using to load is in a different jar to the class you want, then it would otherwise fail with class.forname. i suggest using the context version as default since you never know where your code will end up. this is particually important when working with application containers
I don't think so.
Class.forName() can load any class from any jar that exist in the class path,