Solved

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

Posted on 2008-10-16
5
849 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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

705 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