tags:

views:

1790

answers:

4

I have two applications under tomcat/webapps folder.

tomcat/webapps/App1 and tomcat/webapps/App2
both applications share the same libraries. Which are sored for example in tomcat/webapps/App1/WEB-INF/lib.

Are same libraries loaded twice on memory?

Should i put these shared libraries in tomcat/server/lib?

Thanks for the feedback

SOLUTION

If you do not want your libraries to load twice put them in:

  • Tomcat 6 $CATALINA_HOME/lib
  • Tomcat 5 $CATALINA_HOME/common/lib
+2  A: 

Hi,

As you can see here, Tomcat creates one class-loader per webapp on your server. Thus, if you have webapp1 and webapp2 that share the same library, then this library will be indeed loaded twice.

You can eventually place this library in the common directory (tomcat-dir/common/lib) if it is shared by all webapps that run on your Tomcat server.

romaintaz
i do not have a common directory inside server, I just have classes and lib directory. Should i create one common inside?
Sergio del Amo
I edited my post to indicate that the good directory to use is [tomcat-installation-directory]/common/lib
romaintaz
A: 

From experience: the two web-apps are entirely isolated from one another - the libraries for one are not utilised in another - thus to answer your initial question - yes they would be loaded twice.

To answer you second question, whether you should deploy these libraries into Tomcat's shared directory - I would say no, and here's why:

If you deploy a library Jar into the shared location (tomcat/server/lib), then that version of the library becomes the default for all web-applications running under that instance of Tomcat. As you can see from this overview of the tomcat architecture, the class-loader works "down the chain", with an individual web-app's lib folder being the last place it will look before it throws a class-not-found exception.

The problem therefore of deploying a shared library to that directory is that it breaks the architecture for individual applications being isolated from one-another. Fine in your initial example, but if you want to deploy a third-party application (e.g. if you a running an app that consumes Portlet's to handle specific content), you instantly run in to version dependency issues - your shared version of a library may not be correct for the third-party application, but because the package is already loaded, you'll throw exceptions left right and centre.

iAn
A: 

i do not have a folder server/common, I just have server/classes and server/lib directory. Should i create one common folder inside?

Sergio del Amo
Sorry, there was a mistake in my answer.When I said "server/common/lib", I meant "tomcat-server-directory/common/lib".
romaintaz
tomcat5 had SERVER_HOME/common/lib. tomcat version 6 has SERVER_HOME/lib.
Steve B.
+2  A: 

I wouldn't recommend placing the jar files in the shared folder. Let's say for example that you need in the future to deploy a third party application, that has a newer version of a jar file in the WEB-INF folder. For this application the classes of the jar will be loaded twice (even if they have the same names), one from the shared folder and one from the web app folder. This situation may cause bugs very difficult to find.

If the jar files are in the web app folders, then they are loaded by separate class loaders and don't interfere with each other.

kgiannakakis
I quite agree. Placing libraries in commons directory can be dangerous, and must be used only if you can control which webapps are deployed, and what are the version of libraries used for each webapp...
romaintaz