Difference Between Jar and War in Java

Difference between jar and war in Java

From Java Tips: Difference between ear jar and war files:

These files are simply zipped files
using the java jar tool. These files are
created for different purposes. Here
is the description of these files:

  • .jar files: The .jar files contain
    libraries, resources and
    accessories files
    like property files.

  • .war files: The war file contains
    the web application
    that can be
    deployed on any servlet/jsp
    container. The .war file contains jsp,
    html, javascript
    and other files
    necessary for the development of web
    applications.


Official Sun/Oracle descriptions:

  • The J2EETM Tutorial: Web Application Archives
  • The Java Archive (JAR) File Format: The Basics

Wikipedia articles:

  • WAR file format (Sun)
  • JAR file

Differences between jar and war in Spring Boot?

Spring Boot can be told to produce a 'fat JAR' which includes all of your module/service's dependencies and can be run with java -jar <your jar>. See "Create an executable JAR with Maven" here.

Spring Boot can also be told to produce a WAR file, in which case you'll likely choose to deploy it to a web container such as Tomcat or Jetty.

Plenty more details on Spring Boot deployment here.

JAR vs WAR file specification

From a conceptual point of view:

A jar typically represents a single library which can be used and may or may not have dependencies.

But the idea is that a specific functionality can be provided as a library in a jar.

A war is an application by itself and as such it should include all the dependencies

Difference between Jar and War packaging with embedded Tomcat

Generally speaking, when you're using an embedded container, you should stick to the defaults and build a jar file.

There are, however, two exceptions to that rule:

  1. You want to use JSPs. In that case you'll need to use war packaging as Tomcat's JSP support is tightly coupled to the layout of a war file.
  2. You want to have the option of also deploying your application to a standalone container or app server.

.war vs .ear file

From GeekInterview:

In J2EE application, modules are packaged as EAR, JAR, and WAR based on their functionality

JAR:
EJB modules which contain enterprise java beans (class files) and EJB deployment descriptor are packed as JAR files with .jar extension

WAR:
Web modules which contain Servlet class files, JSP Files, supporting files, GIF and HTML files are packaged as a JAR file with .war (web archive) extension

EAR:
All the above files (.jar and .war) are packaged as a JAR file with .ear (enterprise archive) extension and deployed into Application Server.



Related Topics



Leave a reply



Submit