'Difference between jar and war in Java

What is the difference between a .jar and a .war file?
Is it only the file extension or is there something more?



Solution 1:[1]

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:


Wikipedia articles:

Solution 2:[2]

WAR stands for Web application ARchive

JAR stands for Java ARchive

enter image description here

Solution 3:[3]

A .war file has a specific structure in terms of where certain files will be. Other than that, yes, it's just a .jar.

Solution 4:[4]

You add web components to a J2EE application in a package called a web application archive (WAR), which is a JAR similar to the package used for Java class libraries. A WAR usually contains other resources besides web components, including:

  • Server-side utility classes (database beans, shopping carts, and so on).
  • Static web resources (HTML, image, and sound files, and so on)
  • Client-side classes (applets and utility classes)

A WAR has a specific hierarchical directory structure. The top-level directory of a WAR is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static web resources are stored.

(source)

So a .war is a .jar, but it contains web application components and is laid out according to a specific structure. A .war is designed to be deployed to a web application server such as Tomcat or Jetty or a Java EE server such as JBoss or Glassfish.

Solution 5:[5]

A .war file is a Web Application Archive which runs inside an application server while a .jar is Java Application Archive that runs a desktop application on a user's machine.

Solution 6:[6]

A war file is a special jar file that is used to package a web application to make it easy to deploy it on an application server. The content of the war file must follow a defined structure.

Solution 7:[7]

.jar and .war are both zipped archived files. Both can have the optional META-INF/MANIFEST.MF manifest file which hold informative information like versioning, and instructional attributes like classpath and main-class for the JVM that will execute it.

.war file - Web Application Archive intended to be execute inside a 'Servlet Container' and may include other jar files (at WEB-INF/lib directory) compiled classes (at WEB-INF/classes (servlet goes there too)) .jsp files images, files etc. All WAR content that is there in order to create a self-contained module.

Solution 8:[8]

war and jar are archives for java files. war is web archive and they are running on web server. jar is java archive.

Solution 9:[9]

Basicly both compressed archives. war is used for web application with a specific directory structure.

Solution 10:[10]

A JAR file extension is .jar and is created with jar command from command prompt (like javac command is executed). Generally, a JAR file contains Java related resources like libraries, classes etc.JAR file is like winzip file except that Jar files are platform independent.

A WAR file is simply a JAR file but contains only Web related Java files like Servlets, JSP, HTML.

To execute a WAR file, a Web server or Web container is required, for example, Tomcat or Weblogic or Websphere. To execute a JAR file, simple JDK is enough.

Solution 11:[11]

War -

distribute Java-based web applications. A WAR has the same file structure as a JAR file, which is a single compressed file that contains multiple files bundled inside it.

Jar -

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

WAR files are used to combine JSPs, servlets, Java class files, XML files, javascript libraries, JAR libraries, static web pages, and any other resources needed to run the application.

Solution 12:[12]

JAR files allow to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications.

JAR can be created with any desired structure. In contrast, WAR has a predefined structure with WEB-INF and META-INF directories.

A JAR file allows Java Runtime Environment (JRE) to deploy an entire application including the classes and the associated resources in a single request. On the other hand, a WAR file allows testing and deploying a web application easily.

Solution 13:[13]

War : For web-applications
Jar : For desktop applications

OR

War : Working on browser
Jar : Working on machine

Solution 14:[14]

Jar:- jar contain only .class war:- war contain html, js, css and .class also jsp and servlets pages

Solution 15:[15]

Also, using an embedded container, you can run a JAR file directly whitouh setting up a web server like when running your java app with spring boot. However, for a WAR file, you need to set up first a web server like Tomcat for example.

Solution 16:[16]

[1]JAR Packaging ::

Simply put, JAR – or Java Archive – is a package file format. JAR files have the .jar extension and may contain libraries, resources, and metadata files.

Essentially, it's a zipped file containing the compressed versions of .class files and resources of compiled Java libraries and applications.

For example, here's a simple JAR file structure:

META-INF/ MANIFEST.MF com/ baeldung/ MyApplication.class The META-INF/MANIFEST.MF file may contain additional metadata about the files stored in the archive.

We can create a JAR file using the jar command or with tools like Maven.

[2] WAR Packaging ::

WAR stands for Web Application Archive or Web Application Resource. These archive files have the .war extension and are used to package web applications that we can deploy on any Servlet/JSP container.

Here's an example layout of a typical WAR file structure:

META-INF/ MANIFEST.MF WEB-INF/ web.xml jsp/ helloWorld.jsp classes/ static/ templates/ application.properties lib/ // *.jar files as libs Inside, it has a META-INF directory holding useful information in the MANIFEST.MF about the web archive. The META-INF directory is private and can't be accessed from the outside.

On the other hand, it also contains the WEB-INF public directory with all the static web resources, including HTML pages, images, and JS files. Moreover, it contains the web.xml file, servlet classes, and libraries.

We can use the same tools and commands that we used to build a JAR to build a .war archive.

[3] Differences ::

The first and most obvious difference is the file extension. JARs have the .jar extension, whereas the WAR file has the .war extension.

The second main difference is their purpose and the way they function. JAR files allow us to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications.

The structure of the archives is also different. We can create a JAR with any desired structure. In contrast, WAR has a predefined structure with WEB-INF and META-INF directories.

Finally, we can run a JAR from the command line if we build it as an executable JAR without using additional software. Or, we can use it as a library. In contrast, we need a server to execute a WAR.