1. Overview

Usually, when we need JAR files in our Java projects, we put them in the classpath as external libraries without extracting them. However, we sometimes want to them to our filesystem.

In this tutorial, we’ll explore how to extract a JAR file to a specified directory in the command line. We’ll use Linux and Bash as an example to address each approach.

2. Introduction to the Problem

We’ll use Guava‘s JAR file as an example in this tutorial. The latest release at the time of writing is guava-31.1-jre.jar.

Java ships the jar command to create, update, view, and extract JAR files. Next, let’s extract Guava’s JAR file using the jar command:

$ tree /tmp/test/jarTest
/tmp/test/jarTest
└── guava-31.1-jre.jar

0 directories, 1 file

$ pwd
/tmp/test/jarTest

$ jar xf guava-31.1-jre.jar 

$ tree /tmp/test/jarTest   
/tmp/test/jarTest
├── com
│   └── google
│       ├── common
│       │   ├── annotations
│       │   │   ├── Beta.class
...
27 directories, 2027 files

As the output above shows, we can extract a JAR file using jar xf, and the jar command extracts it to the current directory by default.

However, the jar command doesn’t support extracting the file to a specified directory.

Next, let’s see how to achieve this in action.

3. Entering the Target Directory Before the Extraction

We’ve learned that the jar command extracts the given JAR file to the current working directory by default.

So, an idea to solve the problem comes up: if we want to extract the JAR file to a specified target, we can first enter the target directory and then launch the jar command.

Next, let’s determine whether this idea works as expected.

3.1. Testing the Idea

First, let’s create a new directory /tmp/test/newTarget:

$ mkdir /tmp/test/newTarget

$ tree /tmp/test/newTarget
/tmp/test/newTarget
0 directories, 0 files

Next, let’s enter the target directory and then extract Guava’s JAR file:

$ cd /tmp/test/newTarget && jar xf /tmp/test/jarTest/guava-31.1-jre.jar 
$ tree /tmp/test/newTarget
/tmp/test/newTarget
├── com
│   └── google
│       ├── common
│       │   ├── annotations
│       │   │   ├── Beta.class
...

As the example above shows, the idea works. We’ve extracted Guava’s JAR file to our desired directory.

However, if we revisit the commands we’ve executed, we realize there are some inconveniences:

  • We must first create the target directory manually if it’s a new one.
  • As the cd command changes our current working directory, we must pass the JAR file with its absolute path.
  • After we’ve executed the command, we are left in the target directory instead of where we started.

So next, let’s see how we can improve our solution.

3.2. Creating the xJarTo() Function

We can create a shell function to automatically create the directory and adapt the JAR file’s path. Let’s first have a look at the function, and then understand how it works:

#!/bin/bash

xJarTo() {
    the_pwd="$(pwd)"
    the_jar="$1"
    the_dir="$2"

    if [[ "$the_jar" =~ ^[^/].* ]]; then
        the_jar="${the_pwd}/$the_jar"
    fi
    echo "Extracting $the_jar to $the_dir ..."
    mkdir -p "$the_dir"
    cd "$the_dir" && jar xf "$the_jar"
    cd "$the_pwd"
}

The function first stores the user’s current working directory (pwd) in the the_pwd variable.

Then, it checks the JAR file’s path:

  • if it starts with “/” –  absolute path, so we just use the user input directly
  • otherwise – relative path. We need to prepend the user’s current working directory to build the absolute path to work with

Therefore, the function adapts both absolute and relative JAR paths.

Next, we execute the mkdir -p command before entering the target directory. The -p option tells the mkdir command to create missing directories in the given path if there are any.

Then, we enter the target directory using cd and execute the jar xf command with the prepared JAR file path.

Finally, we navigate back to the user’s current working directory.

3.3. Testing the Function

Now that we understand how the xJarTo() function works, let’s source the function and test if it works as expected.

First, let’s test the scenario that the target directory is new and the JAR file is an absolute path:

$ pwd
/tmp/test

$ xJarTo /tmp/test/jarTest/guava-31.1-jre.jar /tmp/a_new_dir
Extracting /tmp/test/jarTest/guava-31.1-jre.jar to /tmp/a_new_dir ...

$ tree /tmp/a_new_dir
/tmp/a_new_dir
├── com
│   └── google
│       ├── common
│       │   ├── annotations
│       │   │   ├── Beta.class
...
$ pwd
/tmp/test

As the output above shows, the function works as expected. The new directory is created on the fly, and the extracted content is under /tmp/a_new_dir. Further, we’re still under /tmp/test after calling the function.

Next, let’s test the relative JAR path scenario:

$ pwd
/tmp/test/jarTest

$ xJarTo guava-31.1-jre.jar /tmp/another_new_dir
Extracting /tmp/test/jarTest/guava-31.1-jre.jar to /tmp/another_new_dir ...

$ tree /tmp/another_new_dir
/tmp/another_new_dir
├── com
│   └── google
│       ├── common
│       │   ├── annotations
│       │   │   ├── Beta.class
...

$ pwd
/tmp/test/jarTest

This time, as we’re under /tmp/test/jarTest, we pass guava-31.1-jre.jar directly to the function. And it turns out the function does the job as well.

4. Using Other Unzip Commands

JAR files use ZIP compression. Therefore, all unzip tools can extract JAR files. If a unzip utility supports extracting to a specified directory, it solves the problem.

This approach requires at least one unzip tool available on our system. Otherwise, we need to install it. It’s worth mentioning that, installing packages on a system usually requires root or sudo privileges.

So next, let’s take unzip as an example to show the installation commands for the popular yum and apt package managers:

# apt
sudo apt install unzip

# yum
sudo yum install unzip

The unzip command supports the -d option to extract the ZIP archive to a different directory:

$ unzip guava-31.1-jre.jar -d /tmp/unzip_new_dir
$ tree /tmp/unzip_new_dir
/tmp/unzip_new_dir
├── com
│   └── google
│       ├── common
│       │   ├── annotations
│       │   │   ├── Beta.class
...

As we can see, unzip -d extracts the JAR file to the specified directory.

5. Conclusion

In this article, we’ve learned two approaches to extracting a JAR file to a specified directory.

To achieve that, we can create a shell function to wrap the standard JAR command. Alternatively, if we have other unzip tools available on the system, such as unzip, we can use their corresponding option to extract the JAR content to the desired directory.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.