Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a jar file is required by multiple web applications?

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?

like image 554
chows2603 Avatar asked Oct 19 '22 15:10

chows2603


1 Answers

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.

See also:

  • How do I include a JSP file from a different project into my project
  • Structure for multiple JSF projects with shared code
  • @FacesComponent on shared library
  • How to correctly use OmniFaces in an EAR
  • Exception: could not find Factory: javax.faces.context.FacesContextFactory
  • To prevent a memory leak, the JDBC Driver has been forcibly unregistered
like image 100
BalusC Avatar answered Oct 21 '22 06:10

BalusC