Peter Donald wrote:
> At 04:08 21/1/01 -0800, Craig R. McClanahan wrote:
> >Sealing is one "user error" issue that will cause classloading to fail inside
> >Tomcat. Another is the fact that a particular class can only see other
> classes
> >in its own classloaders, and parents of that classloader, but not
> children. The
> >net effect is that some class libraries will NOT work unless you put them
> inside
> >WEB-INF/lib, because they need to be able to access other classes specific to
> >that webapp.
> >
> >Consider the following scenario:
> >* Class L is in a shared library ($TOMCAT_HOME/lib for Tomcat)
> >* Class W is in WEB-INF/classes or WEB-INF/lib/*.jar
> >* You pass the fully qualified class name of class W to a
> > method in class L whose job is to instantiate a new object
> > of this class.
> >* Class L does essentially the following:
> > Class wClass = Class.forName(wClassName);
> > and encounters ClassNotFoundException.
> >
> >The reason for the exception is that Class L was loaded from the shared
> library
> >classloader (which is the parent of all webapp classloaders in Tomcat).
> >Therefore, it cannot see into the webapp to find class W.
>
> I was under the impression that Class.forName() loaded classes from the
> context classloader.
Do you mean the one you set with Thread.setContextClassLoader()? That's not the
way the standard ones work, although you could write such a beast (of course, this
is specific to Java2 as well).
The precise answer to what works depends on how your particular class loader is
implemented. My experience (although this classloading stuff is "black magic" at
times) is that URLClassLoader, which is what Tomcat 4.0 uses, follows the process
described in the javadocs for java.lang.ClassLoader.loadClass():
* Call findLoadedClass(String) to see if this class
has already been loaded by this class loader.
* Call loadClass() on the parent class loader of
this class loader (NOTE: if found, then that class's
classloader will be our parent, not us).
* Attempt to load the class from our own repositories
(i.e. the list of URLs it was initialized with for a
URLClassLoader).
> So in theory the above would work if context
> classloader was set up correctly. The only thing that wouldn't work is
> something like
>
> Class wClass = getClass().getClassLoader().loadClass( wClassName );
>
> Cheers,
>
> Pete
>
Craig
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]