Difference Between JAR and WAR

JAR and WAR are two types of file archives. More correctly, a WAR file is also a JAR file, but they are used for different purposes. JAR files are like well known ZIP files. They could be used for any general purpose archiving, but the most popular use of JAR files is using them as containers for Java class files and resource files that make up a java application. WAR files are used specifically for the deployment of web applications.

What is JAR?

JAR (Java ARchive) is a file archive that carries many other files. JAR files are usually used by Java developers to distribute Java applications or Java libraries using the JAR files as containers for the Java class files and corresponding resource files (i.e. text, audio, video, etc.). Well known file archiving format ZIP is the basis on which the JAR file is built on. Users can either use the jar command of the JDK (Java Development Kit) or regular ZIP software for extracting the contents of JAR files. JAR files are a very convenient way of downloading an entire web application in a single file, without having to download all the files that make up the web application separately. In order to read/write JAR files, Java developers use the classes contained in java.util.zip package. If the JAR file is supposed to be executed as a stand-alone application, then one of the classes will be specified as the “main” class within the entries of the manifest file. Executable JAR files can be run using the java command with the jar attribute (i.e. java -jar foo.jar).

What is WAR?

WAR (Web Application aRchive) is a JAR file used as a container for a group of web application resource files (that make up a web application) such as JSP (Java Server Pages), servlets, class files, XML files and web (HTML) pages. WAR files are identified by their .war file extension. They were developed by Sun Microsystems (original developers of Java programming language). Digital signatures used on JAR files (to entrust the code) can be used on WAR files as well.

A WAR file is internally organized in to a hierarchy of special directories. The structure of the web application contained in the WAR file is defined in the web.xml file (which resides inside the /WEB-INF directory). Web.xml also describes which URL is connected with which servlet. They also define the variables that are accessible inside the servelet and dependencies that must be set up. However, if the WAR file only contains JSP files, then the web.xml file is optional.

What is the difference between JAR and WAR?

JAR files have the .jar file extension, while WAR files have the .war extension. But, a WAR file is a specific type of a JAR file. JAR files contain class files, libraries, resources and property files. WAR files contain servlets, JSP pages, HTML pages, JavaScript coding. JAR files are used to archive a whole Java (desktop) application, while WAR files are used to deploy web applications.