JAR file specification vs WAR

I noticed that the WAR files must be catalogs classes/

and lib/

for their root directory and dependencies, respectively.

I also noticed that it is not common practice for JARs to contain such a directory lib/

and contain their own dependencies.

So now I'm wondering why JARs shouldn't / usually don't contain their own dependencies, but WAR files are expected to be. Unless I'm missing something, both require their dependencies to be on the classpath at runtime (JARs don't run unless they respect dependencies, just like WARs don't run). So for me all the arguments for installing dependencies in the WAR file also apply to the JAR.

What am I not "getting" here?!?


source to share

4 answers

A is jar

usually a single library that can be used and may or may not have dependencies.
But the idea is that certain functionality can be provided as a library in jar


A war

is an application in itself and as such should include all dependencies



These are containers for different purposes.

  • Jar

    is for Java

    class files and resources and can be a library or a directly executable application.

  • War

    is a container for bundled web applications , including all dependencies (for example, Jar files and other web resources).

The question comes down to why Jar

can't include other Jar

s. This I would take as a design decision made for maximum flexibility in interchangeability.



WAR files (mostly WEB applications) are uncompressed and all JAR files inside the lib folder will be in the classpath of your web application at runtime.

JAR files can also contain jar files, but will not be found and loaded by the default class loader.

There are special class loaders that load jar files inside jar files, but not standard ones.



Because unless you are using a custom class loader that can load classes from nested jars (like JarClassLoader ), you cannot load classes from nested jars. Servlet containers and applications will add the jars contained in war / ear files to their application classpath.



All Articles