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.
Suggestion: How to Extract or Unzip Tar Gz File in Linux
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.
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.
Suggestion: How to extract a .7z compressed file in Linux
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.
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.
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?
Suggestion: How to unpack initrd/initramfs to view content in Linux
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.
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
#OR
$ unzip -Zl sample.war
Below is the output of the above command.
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.
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.
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.
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.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.
I am really glad to see that you are putting so much of effort into encouraging the readers with valuable posts like this.
Thank you!