Horaci Macias wrote:

I was reading the article and I found that it is recommended to use this.getClass().getClassLoader().loadClass(String) instead of Class.forName(String).

It says the reason for this is because Class.forName(String) will only work properly if the current class is in the System ClassLoader.

However, in the Javadoc (jdk v1.4.1) for Class.forName(String) says:

"Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:

Class.forName(className, true, currentLoader)


where currentLoader denotes the defining class loader of the current class. (...)"

I think this is the same as "this.getClass().getClassLoader().loadClass(String)" and the javadoc says nothing about the system ClassLoader.

Can anyone clarify this, please?


I can't clarify anything other than to provide an alternative recommendation:


Thread.currentThread().getContextClassLoader().loadClass( "Whatever" );

The problem with the this.getClass().getClassLoader().loadClass(String) is that it triggers a seach relative to the classloader in which "this" is defined. If "this" is in a classloader higher than the class you trying to load then you out of luck wheras the context classloader is the lowest classloader in the stack relative to your implementation.

Stephen.


thank you,


Horaci

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--


Stephen J. McConnell
mailto:[EMAIL PROTECTED]




--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to