?
Solved

Classes in tomcat\server\lib folder sometimes are visible to web application

Posted on 2008-10-16
5
Medium Priority
?
850 Views
Last Modified: 2013-12-02
We are using Tomcat 5.5.17 and are using a custom ClassLoader, which we specify using the Loader element per the documentation at http://tomcat.apache.org/tomcat-5.5-doc/config/loader.html.

We place the jar containing our custom ClassLoader in the tomcat\server\lib folder, per the documentation at http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html.  (I forget why we did this originally, but I think we had problems if we put it elsewhere.)  According to this documentation, classes in this jar should be TOTALLY invisible to web applications.  And indeed, for the most part, if we try to reference some class  in this jar, we get a java.lang.NoClassDefFoundError.  What is strange, however, is that sometimes the web-app does seem to find classes in this jar.   Even more strange, it does not seem to be deterministic.  The app will work for a few days, and then it wont.  So I understand why it does not work (class should not be visible), but why does it ever work?

Does anyone have any thoughts on what might be going on?
0
Comment
Question by:gh5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 20

Assisted Solution

by:chaitu chaitu
chaitu chaitu earned 800 total points
ID: 22737442
put this jar file  tomcat\common\lib folder,
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22737712
Can you post the code of custom classloader?
0
 

Author Comment

by:gh5
ID: 22743442
chaituu, we are doing that as a separate course of action, but that doesn't really address my question.  I am wondering how we ever see the behavior where the class is recognized (i.e. no ClassDefNotFound error) when the jar is in tomcat\server\lib folder
0
 

Author Comment

by:gh5
ID: 22743527
Ajay-Singh: Below is a stripped-down version of our custom classloader -- the rest of the code is comments and debug statements.  Obviously, the heart of the work is done in ClassFileLocationFinder.getClassFileLocations().  I have not included that method, but it just uses our configuration information (file locations indicated by system properties set when launching Java) to determine the urls of the jars to include.  The issue (or non-issue I guess) arises when we try to access ClassFileLocationFinder directly from one of our web-app classes -- it seems that the webapp should never be able to see that class, but sometimes it does, and sometimes it doesn't.


package custom.classloader;

import java.net.URL;
import org.apache.catalina.loader.WebappClassLoader;

public abstract class CustomCatalinaWebappClassLoader extends WebappClassLoader{
 
public CustomCatalinaWebappClassLoader(ClassLoader parent) {
  super(parent);
  init();
  }
 
  private void init(){
      URL[] urls = ClassFileLocationFinder.getClassFileLocations();
      for (URL url : urls){
          this.addURL(url);      
          //super.addRepository(url.toExternalForm());
   }
  }
}


0
 

Accepted Solution

by:
gh5 earned 0 total points
ID: 22956637
We have resolved the issue and it turns out that my initial assertion about our server/lib classes being visible to our web app was incorrect -- it was just that our custom classloader was SOMETIMES loading these classes from a different location, and therefore no error was reported in these cases.  The custom classloader was never actually seeing the classes in server/lib -- it was seein a duplicate for those classes defined elsewhere.  The non-determinism came from a configuration mistake with our custom classloader logic.

Even though the issue is resolved, I would like to award some points to chaituu.  The comment about common/lib didn't resolve the issue, but it did lead to some testing that ultimately led us to figure out the problem.  And this led us to redesign the system in a cleaner way so that we don't have jars containing duplicate classes.
 
Putting the jar in common/lib does not work (and is the reason we avoided it in the first place) because our custom class loader has to extend org.apache.catalina.loader.WebappClassLoaded, which is not visible to common/lib.  However, we split the jar into two, putting the custom ClassLoader into sever/lib and the support classes into common/lib.  Our web-app can now access the support classes without getting errors, and the custom ClassLoader works fine.

How do I go about awarding partial points?

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question