What are WAR files?

A WAR file, commonly known as a web application archive file or web application resource file, is a container to store the resources of a Java web application. WAR files are understood by servletsServlets are Java classes that extend the functionality of web servers to handle requests and responses in a dynamically., which can easily deploy the application. Therefore, they enable software engineers to package, transport, and deploy Java web applications.

Structure of a WAR file

A WAR file typically stores static HTML pages, images, JavaServer pages, Java servlets, XML files, libraries, and other resources that constitute a Java web application. The top level consists of a WEB-INF directory and application-specific content such as Java server pages, static HTML pages, images, etc.

Structure of a WAR file
Structure of a WAR file

The WEB-INF directory stores configurations that are required by the servlets. It consists of the following files:

  • web.xml: This file is commonly referred to as a deployment descriptor. It defines the configurations used by servlet containers to deploy the application. Configurations for listeners, filters, servlets, and initialization parameters are some of the key components in the web.xml file.

  • classes: This directory consists of Java classes, servlet classes, and classes defined in the application.

  • tags: This directory consists of the .tag files required by the modules in the Java web application.

  • lib: This directory consists of the libraries required by the Java web application.

Advantages of a WAR file

WAR files offer the following advantages:

  • Standard structure: WAR files explicitly define a folder for each resource of a Java web application forming a standard structure.

  • Easy deployment: A WAR file is assigned to an application server that manages unpacking and deploying the Java web application.

  • Portable: WAR files enable developers to port Java web applications from one servlet container to another easily.

  • Isolation: A WAR file isolates each Java web application, with its resources, dependencies, libraries, and configurations, from the other. This can avoid conflicts among the applications.

  • Versioning: A WAR file is commonly named as the version of the web application, for example, web_application 1.1.4. This helps developers easily roll back to a previous version.

  • Consistency: WAR files for all the web applications result in a consistent format to store web applications.

Advantages of WAR files
Advantages of WAR files

Why use a WAR file?

A frequently asked question is: Why use WAR files instead of regular zip files? A regular zip file doesn’t have a standard structure. Therefore, when the application is deployed on a servlet container, it can’t unpack and set up the Java web application despite defining configurations and dependencies in the zip file. The developer must manually do the entire setup. On the other hand, servlets containers unpack a WAR file and can conveniently deploy Java web applications with the help of the web.xml file.

How to make a WAR file

We use the jar tool of the JDK, the Java Development Kit, to make a WAR file. The following command creates a WAR file named app.war consisting of all the files in the current directory specified by *.

jar -cvf app.war *

Here, the flag -c specifies that the file is a war file, the -v flag displays the verbose, and -f flag specifies the name of the archive file.

We can manually unpack a WAR file using the following command:

jar -xvf app.war

The -x flag is used to unpack the file.

Run these commands in the terminal provided below. Use the command given below to go through the directory and view the structure of the files:

ls
Terminal 1
Terminal
Loading...

Conclusion

The WAR file format provides a standard container for Java web developers to deploy their Java web applications. The WAR file format is understood by the majority of servlets and they natively know how to set up these applications and conveniently deploy them.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved