----- Original Message ----- From: "Craig R. McClanahan" <[EMAIL PROTECTED]> To: "Tomcat Users List" <[EMAIL PROTECTED]> Sent: Monday, December 17, 2001 1:51 PM Subject: Re: AW: Classloader question
On Mon, 17 Dec 2001, Lauer, Oliver wrote: > > But why wasn't the class found in myapp/WEB-INF/lib ? Sorry, but I don't > understand that !? > Class loaders are black magic :-). Here is one simple scenario to illustrate the kinds of problems you can have. Assume you have class A that uses Class.forName() to dynamically create classes for you. Now assume that class A is in /common/lib, and you pass it the name of a class that is present only in /WEB-INF/lib to create. Because A was loaded from the "Common" classloader, it cannot see classes in your webapp, and you get ClassNotFoundException. As another example of problems you can encounter, assume you have classes A and B in a JAR file, and that you have that JAR file in both /WEB-INF/lib of your webapp, and in /common/lib. Assume that class A has a method foo that takes a B as a parameter. Now, consider the following sequence of events: * Class A gets loaded from /common/lib (because it was first referenced by some other class in /common/lib. * You create an instance of class A and store it in your session attributes. * Class B gets loaded from /WEB-INF/lib (because it was first referenced by some other class in /WEB-INF/lib. * You create a local instance of class B. * Your application retrieves the A object from the session attributes and tries to call foo(), passing the instance of B mentioned above. * You get a ClassCastException error. Why? Because the instance of class A (loaded from /common/lib) can only accept an argument that is an instance of class B *also* loaded from /common/lib). The class B loaded from /WEB-INF/lib is considered to be a different class, even though it has the same name (and perhaps even the same bytes). There are thousands of ways you can trip yourself up like this. In general, you're much better off ensuring that you have one and only one copy of every class visible -- then, you never have to worry about it. Craig Hello Craig, Thanks for your email ! I have a question about ClassLoaders in TOMCAT4.0, is the following right? -way1 for some kinds of classes(for ex., java.lang.String), the ClassLoader of mywebapp will follow the normal ClassLoader Delegation Model -way2 but for other classes(for ex., MySerlvet.class), the ClassLoader of mywebapp will "partly-follow" the normal ClassLoader Delegation Model , i.e., it will first look for the class in WEB-INF/classes and WEB-INF/lib, if it is not there, then goto the normal ClassLoader Hierarchies(Primordial ClassLoader/System ClassLoader...) -way3 or for any class, the ClassLoader of mywebapp will follow the way2? - other ClassLoaders( for server/classes|server/lib, common/classes|common/lib, shared/classes|shared/lib) will completely-follow the normal ClassLoader Delegation Model, is that right? Thanks in advance! Bo Dec.17, 2001 -- To unsubscribe: <mailto:[EMAIL PROTECTED]> For additional commands: <mailto:[EMAIL PROTECTED]> Troubles with the list: <mailto:[EMAIL PROTECTED]>