Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Configuring Build Paths or WEB-INF/lib folder [duplicate]

I've seen a lot of tutorials and applications that put their jars inside a build path while others put it inside their web-inf/lib folder, Are there any significant difference? What are the pros and cons of both? What is an indicator for me to put a certain jar inside the libs folder and put the jar in the build path?

like image 441
user962206 Avatar asked Dec 03 '12 07:12

user962206


2 Answers

An application with WEB-INF folder is a web application. Meaning it will be packed as a WAR file and deployed on a server.

If you are using an IDE like eclipse, and you export the project as a WAR file, it will automatically take jars under the lib folder and pack them with the WAR and that will make them available for the application running on the server (jars under WEB-INF/lib are included in the application classpath).

If you just put them anywhere else and include them in the build path, when you export the project you have to declare that you want the jars in the build path to be included as well.

Basically, there is no big difference but if you know you need this jar in runtime (i.e. after deploying the application to the server), it is better to put it under the WEB-INF/lib folder.

like image 152
Tomer Avatar answered Sep 27 '22 17:09

Tomer


What do you mean by build path? Most of the JARs you use need to be available during building (compiling) because your code depends on them - otherwise it won't compile.

The real question is between placing JARs in WEB-INF/lib vs. your container /lib directory. In general you should always put your JARs inside an application in WEB-INF/lib. Placing them globally has several consequences:

  • singletons are now global to all web applications (one class loader) if multiple deployed

  • it's easier to introduce memory leak if a library holds a reference to any of your classes (see above)

  • you don't duplicate the same classes (each web application will reuse the same class as opposed to having a separate copy in each class loader

like image 29
Tomasz Nurkiewicz Avatar answered Sep 27 '22 17:09

Tomasz Nurkiewicz