Java Configuring Build Paths or WEB-INF/lib folder

13,005

Solution 1

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.

Solution 2

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

Share:
13,005

Related videos on Youtube

user962206
Author by

user962206

Updated on September 16, 2022

Comments

  • user962206
    user962206 about 1 year

    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?

  • Jewels
    Jewels over 9 years
    could you please provide some kind of documentation for this?