Solved

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

Posted on 2008-10-16
5
843 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
  • 3
5 Comments
 
LVL 20

Assisted Solution

by:chaitu chaitu
chaitu chaitu earned 200 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 196
Why does the Android Studio logcat show this error when the user presses the button? 2 46
tomcat administrtor 12 48
hibernate example using maven 12 42
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

809 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