If a jar file is required by multiple web applications then which option will you choose? Keeping it in server classpath or keeping one copy of the jar file for each web application's lib folder?
Technically, it depends on what kind of JAR file it is and usually you have only one option.
If it's a Java EE based "web fragment" JAR file, recognizable by having a /META-INF/web-fragment.xml
file, and/or a /META-INF/faces-config.xml
, and/or a /META-INF/*.tld
file, and/or a /META-INF/resources
folder containing web content files (JSP/CSS/JS/etc), then it definitely belongs in WAR's /WEB-INF/lib
. Otherwise annotated/registered web fragment artifacts (modular servlets, filters, listeners, tags, components, beans, etc) won't be auto scanned, discovered and installed, and/or web fragment resources (shared JSP/Facelets/CSS/JS/image files) can't be included in webapp.
Or if it represents an implementation of a Java EE API, such as JSF, JSTL, JAX-RS, etc, then it can (should) go in server's /lib
, but then you must make sure that you replace any existing/older implementation, otherwise you may run into classloading trouble caused by duplicate different versioned libraries in runtime classpath (recognizable by class/method/field related exceptions such as NoSuchMethodError
, LinkageError
, etc). If you include it in WAR anyway, then you need to make sure that you instruct the server or the API in question to use WAR-bundled implementation instead of the server-bundled one.
Else it's most likely a "plain vanilla" Java SE based library, such as Apache Commons and friends. Such a library can safely go in server's /lib
and be shared among all webapps. This is at least required for JDBC drivers and smiliar JARs having a service loader which auto-loads stuff into memory during JVM startup, recognizable by a /META-INF/services
folder targeted on a Java SE based API. Otherwise memory leak risks may occur during hotdeployments when such a library is placed in webapp's /WEB-INF/lib
, because it can't signal a Java EE undeploy and blindly auto-loads stuff once again while the JVM hasn't shutdown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With