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.
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
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
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
The above command automatically creates the jspforum directory before extracting files into it. We can now comfortably list that directory.
$ ls -l jspforum
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 Or $ unzip -Zl jspforum-annotation.war
After, you have identified the file you wish to extract, implement the command:
$ unzip jspforum-annotation.war FILE_NAME
$ unzip jspforum-annotation.war index.jsp
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.