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

- 33,893
- 13
- 69
- 83

- 59,493
- 71
- 188
- 276
15 Answers
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:
-
17Am having an doubt as the jsp and additional web application files why cant be in jar file. – Emmanuel Angelo.R Dec 10 '13 at 07:18
-
2@EmmanuelAngelo.R They can - a jar file is just a zip file. If it will do anything useful is another matter, which depends on the code you want to utilize the jar file. As an interesting example the Jenkins Continuous Integration server is distributed as a WAR file, which can be run with "java -jar". See https://wiki.jenkins-ci.org/display/JENKINS/Starting+and+Accessing+Jenkins for details. – Thorbjørn Ravn Andersen Nov 03 '16 at 19:50
A .war
file has a specific structure in terms of where certain files will be. Other than that, yes, it's just a .jar
.

- 1,031,962
- 187
- 1,923
- 1,875
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.
-
1So does this mean if you're building a server app that just uses sockets and doesn't have any UI there is no need to package it as a war file? – mal Jan 30 '15 at 08:42
-
Actually now I'm reading a bit more I don't need Tomcat, I can just export the jar to the server and run it directly from there. – mal Jan 30 '15 at 08:42
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.

- 242,243
- 40
- 408
- 536
-
25The second statement is not necessarily true. A jar can also start an embedded web server, e.g. with Spring Boot. – whirlwin Sep 10 '15 at 17:49
-
6even that statement is not entirely true. A jar can also just be a library of code to be used by other applications. In other words it may not even contain a main class for running. – Kevin Welker Nov 12 '15 at 21:02
-
@KevinWelker agreed. Even in 2011(when this post was written) a jar could still just represent a library. However, spring boot wasn't around so I can see overlooking the fact that you can embed a web server in a jar like whirlwind mentioned. – intrepistar_88 Jan 27 '18 at 18:40
-
@Kevin Welker, not "not entirely", I would say "not all truth" as whirlwin just mentioned an exception, which is fully true, isn't it? – Alex Martian Oct 03 '19 at 11:34
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.

- 102,349
- 23
- 137
- 192
.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.

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

- 11,299
- 6
- 47
- 76
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.

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

- 14,361
- 2
- 45
- 50
War : For web-applications
Jar : For desktop applications
OR
War : Working on browser
Jar : Working on machine

- 4,615
- 3
- 44
- 53
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.

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

- 11
- 1
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.

- 1,128
- 13
- 26
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.