3
votes

I am wondering about the following: I have a Tomcat 7 dedicated to a single webapp. Is there a point in putting any jar inside the $TOMCAT_HOME/lib directory? In the documentation it says:

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

  • Bootstrap classes of your JVM
  • System class loader classes (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • Common class loader classes (described above)

So, if I am reading this correctly, there is no reason I should put avtivemq or commons-dbcp. I can instead place them in my webapps lib folder and be absolutely certain that they will be picked up first. If that's so, is there any other scenario that would make sense for me to use the $TOMCAT_HOME/lib directory? If not, could you please explain where is the above flawed?

Thanks,

G.

2

2 Answers

2
votes

If your webapps (or most of them) MUST share the same libraries with the same version (i.e. Database driver or SPI ObjectFactories) you should share them in $TOMCAT_HOME/lib. If there is no solid reason for share libs keep them in their webapp's classloader.

1
votes

Yes. If you restart your web app (e.g. by live uploading a new version or pressing the "restart" button on the tomcat manager console), any jar files in your webapp folder will be flushed and reloaded, but those in the shared lib folder are not reloaded. This means any static values stored in the classes in the jar are not reset, and any threads the jar starts on initialization are not restarted. I don't know about the jar files in the question, but this can be important for other jar files (particularly those that assume they are able to start a daemon thread and not terminate it -- that will cause a context leak if you leave them in your web app folder, which can cause a significant amount of additional memory usage over time if reloads are frequent).