Loading order for jar files

G

Gugle

Hi all,
I have a query regarding the loading order of jar files in Tomcat. I
have 2 jar files and a class 'Test' is present in both of them. 1 jar
file has a newer version of the class file with extra methods. Tomcat
classloader takes only *.jar.(I tried specifying the exact name of the
jar..it didn't load it). Now when I use a method which is present only
in the newer version of the class file, tomcat throws a NoClassDefFound
error since it is loading the older jar. If i delete this old jar file,
then it works fine. So its confirmed that it is loading the older jar
file first. But due to some issues, it is mandatory for me to have both
the jar files. How does Tomcat decide the load order for jar files? Any
help would be most welcome.
 
G

Gabriel Belingueres

You may check:

http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html

Anyway if you have duplicated jars into the same webapp, then deleting
the older one should suffice. In which order does it search the
classloader is irrelevant in this situation, but I don't think Tomcat
load them in some _defined_ order with files in the same /WEB-INF/lib
folder.
Otherwise, I think you should follow the classloader herarchy tree from
bottom up to know the order in which classloaders search for a given
class.

HTH
Gabriel

Gugle ha escrito:
 
D

Daniel Pitts

Gugle said:
Hi all,
I have a query regarding the loading order of jar files in Tomcat. I
have 2 jar files and a class 'Test' is present in both of them. 1 jar
file has a newer version of the class file with extra methods. Tomcat
classloader takes only *.jar.(I tried specifying the exact name of the
jar..it didn't load it). Now when I use a method which is present only
in the newer version of the class file, tomcat throws a NoClassDefFound
error since it is loading the older jar. If i delete this old jar file,
then it works fine. So its confirmed that it is loading the older jar
file first. But due to some issues, it is mandatory for me to have both
the jar files. How does Tomcat decide the load order for jar files? Any
help would be most welcome.

Why do you need both jar files? If they define the same classes, then
you most likely don't need both jar files.
 
G

Gugle

The problem is that another product uses the same common component, but
an older version of it :( ....and for some reason, they cannot upgrade
to the latest version of the jar...

Also, I found another weird workaround...I removed all the jar files,
added only my jar files firsrt and then the older version of the jar
file and restarted tomcat..it seems to work fine now..wondering if it
depends on the order in which the jar files are added to the
directory..
 
D

Daniel Pitts

Gugle said:
The problem is that another product uses the same common component, but
an older version of it :( ....and for some reason, they cannot upgrade
to the latest version of the jar...

Also, I found another weird workaround...I removed all the jar files,
added only my jar files firsrt and then the older version of the jar
file and restarted tomcat..it seems to work fine now..wondering if it
depends on the order in which the jar files are added to the
directory..

You're product and the other product should live in different spaces,
problem solved :)
I actually don't know how tomcat works (I use resin), but I would be
willing to guess that you can install your webapp in a different
directory (or use a war file), and then you wont collide with the other
webapps in the container. That's how it works with Resin at least.
 
J

Juha Laiho

If these are in WEB-INF/lib, Tomcat could very well read the files in the
"directory" order -- what the "directory" order is, is then a different
thing, and depends on the underlying filesystem. I could even imagine
a filesystem where the directory order would not be constant from one
directory traversal to another (but in practice, I cannot name any
filesystem that would work this way).

For the other Tomcat classloaders, I've at least imagined that the
order in which the jar files are listed in catalina.properties is
significant.
Your product and the other product should live in different spaces,
problem solved :)

That's pretty much the correct answer. The two applications (with
conflicting requirements) should be two separate webapps, and
the conflicting jar files should be private to the applications
(so, should reside in WEB-INF/lib for each application).
I actually don't know how tomcat works (I use resin), but I would be
willing to guess that you can install your webapp in a different
directory (or use a war file), and then you wont collide with the other
webapps in the container. That's how it works with Resin at least.

Tomcat has quite a complex classloading machinery (which, if I understand
correctly, is required by the servlet specification). Each webapp
has a private classloader responsible for classloading from within
WEB-INF/lib (and for loading resource files within the webapp).
In addition, there are common, shared, and server classloaders.
Common classloader will be for loading resources that are intended to
be common for all webapps, but not intended to be visible for the
Tomcat container itself. Server classloader, on the other hand, is
meant for resources that are provided for the container, but are not
for use from within the webapps. Shared classloader, finally, is one
which will load resources to be used both in the container and in
webapps.

There's a tutorial for the classloaders in the Tomcat documentation,
but really understanding the issues will take some experimentation
(f.ex. to understand why JDBC drivers used with connection pooling
must be provided by "shared classloader", instead of having the
pooling server-side classes provided by server classloader, and
application side by webapp classloader).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top