In this short tutorial, we’ll take a look at several ways to extract the contents of a WAR file in Linux.
2. Using unzip
Let’s start with a really simple example and extract the contents of a WAR file into the current working directory:
If we then list the contents of our working directory we’ll see the contents of our WAR:
META-INF WEB-INF css favicon.ico images index.html ...
Although this approach works, it can leave our working directory a little messy, especially if we have many other files inside our WAR or working directory.
To combat this, we can unzip our WAR file directly into a different directory using the -d option to specify a directory location:
unzip baeldung-web-1.0.0.war -d /tmp/baeldung
We can also extract an individual file from the WAR, which can be very useful when debugging:
$ unzip baeldung-web-1.0.0.war index.html Archive: baeldung-web-1.0.0.war inflating: index.html
All we need to do is pass the complete filename and path we wish to extract after the archive name. In this example, we have a file called index.html, which is at the root of our archive that we want to extract.
If we specify a non-existent file we’ll receive an error:
$ unzip baeldung-web-1.0.0.war /bad/path/index.html Archive: baeldung-web-1.0.0.war caution: filename not matched: /bad/path/index.html
Finally, if we simply want to peek inside our WAR file before extracting the contents we can use the -l option to list the contents:
$ unzip -l baeldung-web-1.0.0.war | more Archive: baeldung-web-1.0.0.war Length Date Time Name --------- ---------- ----- ---- 0 05-27-2020 11:28 META-INF/ 131 05-27-2020 11:28 META-INF/MANIFEST.MF 0 05-27-2020 11:28 css/ 0 05-27-2020 11:28 css/images/ 0 05-27-2020 11:28 css/fonts/ 0 05-27-2020 11:28 images/ ... --------- ------- 53893811 974 files
As we can see, using this option lists all the files within the WAR along with the timestamp. It also includes other basic details, such as the file size and the total number of files.
We should note that unzip is not available on most Linux flavors by default, but can be easily installed using the appropriate package manager.
3. Using jar
If we end up on a machine with Java installed and no other archiving software available, we can make use of the jar command. Similar to unzip, jar is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats.
Now we’ll walk through the same set of examples as in the previous section using our baeldung-web-1.0.0.war archive file.
Again, let’s get started by extracting the contents of our WAR file into the current working directory:
jar -xf baeldung-1.0.0.war
We’re using the x option for extract and the f option to specify the archive filename. A little unnervingly, we won’t see any output to the console.
However, we can add the v option which generates verbose output to the standard output:
jar -xvf baeldung-1.0.0.war
Unfortunately, the jar command does not provide an option for specifying the output directory. For this, we’ll have to navigate to our chosen folder before extracting the WAR directly.
Similarly to unzip, we can also extract an individual file from our WAR by passing the filename and path as an argument:
$ jar -xvf baeldung-1.0.0.war index.html inflated: index.html
Furthermore, we can also use the jar command to list the contents of our WAR:
jar -tvf baeldung-web-1.0.0.war
Using the t option allows us to print out the contents of what is inside our archive.
In this brief tutorial, we’ve looked at several alternatives for extracting and viewing the contents of our WAR files on the Linux platform.
We should also not forget to mention the tar utility, which is also popular on Linux systems. We’ve covered this utility in detail in a previous tutorial.