Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted jar files in tomcat/lib or WEB-INF/lib

Tags:

java

jar

tomcat

I am currently working on an application which is around a decade old. When I looked in to the jar files which are associated with the application , I can see lots of jars which are not required and lots of different versions of same jar.

What are the cons of having unwanted jars in lib. What is the easy way to find and remove them?

like image 938
Umesh Avatar asked Sep 17 '09 05:09

Umesh


People also ask

Where do I put JAR files in tomcat?

Tomcat JAR deployment options Package the JAR file in the WEB-INF\lib folder of the Java web application; Place the JAR file in the \lib subfolder of the Apache Tomcat installation; Configure a folder for shared JAR files by editing Tomcat's common.

What is Lib folder in tomcat?

home}/lib folders (which by default are equal to the lib folder of your Tomcat installation) are loaded by the common classloader. Classes in the WEB-INF/classes and jars in the WEB-INF/lib folder (or WAR archive) are loaded by the web application class loader.

Do we need to deploy JAR file in tomcat?

Apache tomcat is a web container you cannot deploy a jar in tomcat server. If you created a web application then export your application as war file and put it in tomcat webapp directory, start the server and your war will be deployed.


2 Answers

You might get problems if a class loader decides to load classes from different version of jar file than you expect. These kind of problems are usually hard to track. Server will look through jars in some specific order (alphabetically by filename?) and use the first matching class/resource it finds. There are probably no guaranties that the newest version of a jar file is looked first.

I do not know of any tools that would find out which jars are unused. That may be impossible in general case due to reflection, but some degree of automatic checking should be possible, at least in theory.

Removing unused jar file by trial and error is problematic if you do not have good unit tests, which I doubt you have in your decades old application. There may always be some rare error case that depends on just the old jar version you just removed.

like image 200
Juha Syrjälä Avatar answered Oct 23 '22 11:10

Juha Syrjälä


My suggestion is that if your applications are working, leave the jars as they are now. It will be very troublesome if not impossible to find out, which jar files are actually picked up by the class loader. (Of course do not deploy new applications).

You can find which packages a jar file contains by using JarAnalyzer. This will give you an idea of duplicates and you can start removing older versions of jar files. However this procedure needs to be done by hand and by trial and error. Also, the same package may be contained in two jars with different names and no versioning information.

As already said having different versions of the same jar could bring havoc. You can't know which version will actually be used and what conflicts it will bring to this or another application. That's why you should never use Tomcat's shared folder for applications jars. If you are in a situation, where many jar files are placed in the shared folder, avoid using this server for new applications.

like image 23
kgiannakakis Avatar answered Oct 23 '22 10:10

kgiannakakis