How to Extract/Decompress a WAR File in Linux

If you are familiar with programming languages, specifically Java, then you might be aware of JAR or WAR terms. However, many of you may not have heard of this term or will encounter it in the near future.

Today, you will learn what they are, why you should use them, and how to extract/decompress a WAR file in Linux.

What is a WAR File?

In a nutshell, a WAR file is an archive or collection of files for web application content that holds files like JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML, CSS, JavaScript), and other resources.

Why do you need them? The short answer is speed. Compressing multiple files into one archive makes it incredibly fast to transfer a single file from client to server which saves a lot of time.

Are there any differences between the WAR file and the JAR file? Both files are compressed files created using the Java JAR tool. However, their roles are different from each other.

  • JAR file: The .jar file includes property files like those contained in libraries, resources, and accessories files.
  • WAR File: Accommodates all portions of web applications like JSP, HTML, JavaScript, and other necessary files that can be deployed on any server/JSP container.

Below is the basic chart to make it easier for you to understand their differences.

JAR and WAR Differences
JAR and WAR Differences

After knowing the usage of this file, you might be handling those compressed files. So, how you can extract/decompress them?

Extracting WAR Files Using the Unzip Tool in Linux

In Linux, you get a popular compression and decompression tool known as Unzip. It is enough for you to extract/decompress any JAR or WAR file without needing any extra tools.

To demonstrate to you with an example, I’ve already had a sample WAR file in my system. To extract the resources from this, I will pass the following command with the location of a WAR file.

$ unzip sample.war

Below is the output of the above command.

Extracting WAR file using Unzip Utility
Extracting WAR file using Unzip Utility

Tada!!! You did a great job, but there is still one problem with this method. If you run the above command in a location full of files and a directory, then it will make it hard to distinguish between the old files and the WAR content file, as shown below.

Extracted WAR files and directory
Extracted WAR files and directory

In my case, the existing and extracted files are limited, which makes it a little less inconvenient to distinguish them. However, for you, the situation might be different, so what should we do?

The answer is simply to create a separate directory for extracting the content. To do so, you can pass the below command with the directory name you wish to extract content in, as shown below.

$ unzip sample.war -d sampledir

In my case, the above command will extract the sample.war file content inside the sampledir directory.

Extract the WAR file content in single directory
Extract the WAR file content in single directory

Now there is one more question! What if the WAR file is big in size and you are interested in a specific file? For that, you need to first check the content of the WAR file by passing the following command.

$ unzip -l sample.war
$ unzip -Zl sample.war

Below is the output of the above command.

Listing the content of WAR file
Listing the content of the WAR file

After you identify the file you are interested in (for me it was index.html), specify it while extracting, as shown below.

$ unzip sample.war index.html

Below is the output of the above command.

Extracting Specific file from WAR file
Extracting specific files from WAR files

What if you have multiple files in your mind to extract them together? Specify each file with space as a separator.

$ unzip sample.war index.html hello.jsp

Below is the output of the above command.

Extracting multiple files from WAR file
Extracting multiple files from the WAR file

What if you want to extract the directory from the WAR file? Specify the directory with an asterisk symbol (*) to define the extraction of all content within it, as shown below.

$ unzip sample.war images/*

The above command will only extract the images directory and all content within it.

Extracting directory from the WAR file
Extracting directories from the WAR file

Let’s take a pause here. I hope you get an idea of how useful the unzip tool is in Linux. However, if you have any more questions left in your mind, then do leave them in the comment section.

This Post Has 2 Comments

  1. Philippa Norman

    I am really glad to see that you are putting so much of effort into encouraging the readers with valuable posts like this.

    1. Gagan Mishra

      Thank you!

Leave a Reply