Home Linux Commandline Tools How to Extract a WAR File in Linux

How to Extract a WAR File in Linux

If you have had sufficient exposure to the world of software engineering, a WAR file; also known as Web application ARchive or Web Application Resource file, should not be a new concept. However, for those of us running into this concept for the first time, a proper introduction to this file archiving terminology is needed.

What is a WAR File?

The simplest definition of a WAR file is an archive that accommodates all portions of a web application from resources like images, JSPs, and HTML pages, to web services and servlets Java class files.

We can also structurally look at a WAR file the same way we look at a JAR file; which is simply a fancy ZIP file. However, in this WAR file to JAR file comparison, the WAR file is specified with the Java code directories and its archives.

Also, this file archive tends to have a web.xml file (designated configuration file) which briefs the application server on the order and manner it should run things.

Finally, a WAR archive can be identified through its .war file extension.

This article will walk us through an effective approach to successfully extracting a WAR file on a Linux operating system environment.

Problem Statement

For this tutorial to practically and efficiently accomplishing its objective of extracting a WAR file in Linux, we will need an already existing sample WAR file for reference.

Consider the following Java EE application file archived in a WAR file format.

$ ls -l jspforum-annotation.war 
Sample WAR File
Sample WAR File

We will now look at an effective way of extracting it through the Linux command-line environment.

Extracting WAR File Using unzip Utility in Linux

According to its manual page, unzip is effective in listing, testing, and extracting compressed content within a ZIP archive. However, this utility is also effective in extracting files from WAR archives.

In reference to the sample WAR file we mentioned earlier, the simplest implementation of unzip utility in its extraction is as follows:

$ unzip jspforum-annotation.war  
Extract WAR File in Linux
Extract WAR File in Linux

Despite unzip being successful in extracting our WAR file, we are forced to deal with a messy working directory which makes it difficult to identify our extracted files and directories, especially in scenarios where the working directory is thoroughly populated with many other files.

To fix the above issue, we can implement the unzip command and also specify the directory destination for accommodating the extracted files by including the -d flag in the command.

$ unzip jspforum-annotation.war -d jspforum
Extract WAR File to Directory
Extract WAR File to Directory

The above command automatically creates the jspforum directory before extracting files into it. We can now comfortably list that directory.

$ ls -l jspforum
List WAR Files in Linux
List WAR Files in Linux

Supposing you are only interested in extracting an individual file from the WAR archive? Here, you will first need to list the content of the archive using unzip in the following manner:

$ unzip -l jspforum-annotation.war
$ unzip -Zl jspforum-annotation.war
View Contents of WAR File in Linux
View Contents of WAR File in Linux

After, you have identified the file you wish to extract, implement the command:

$ unzip jspforum-annotation.war FILE_NAME

For instance,

$ unzip jspforum-annotation.war index.jsp 
Extract One File From WAR Archive
Extract One File From WAR Archive

The unzip utility is an effective tool for extracting WAR archives in a Linux environment since it comes pre-installed on all major Linux operating system distributions.

Hope this article guide helped solve your WAR file extraction problem. Feel free to leave a comment or feedback.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.