Packagecloud logo

Inspecting and extracting JAR files from the command line

Overview

This post covers how to use `jar` or `unzip` command-line to manipulate 'jar' files.`jar' is native to JAR files and is recommended but `unzip` is available by default on most operating systems.

  • Extract contents of a JAR file
  • List files in a JAR file without extracting them
  • Extract specific files from a JAR file
  • Extract contents of a JAR file without `jar` command line tool (using `unzip`)
  • List files of a JAR file without extracting them without `jar` command line tool (using `unzip`)
  • Extracting contents of a JAR file to specific directory

What is a JAR file?

A JAR file is identical to a zip file format. Both compresses a group of files (including directories) into a single file to reduce the total size, as well as retain the directory structure of the files.

Why use a JAR file?

As a JAR file compresses a group of files while retaining directory structure of the files into a single file, it makes it extremely easy to share your directory of files as a single file, and it also requires less network bandwidth to transmit/share the file and store for further usage.

When is JAR file used?

 JAR file is used as the de-facto standard to package your code written in Java as:

  • A standalone application
  • A web application (also known as a WAR file), used in a Java Enterprise Edition Web Profile-compliant application server, like Apache Tomcat.
  • An enterprise java application (also known as an EAR file), used in a Java Enterprise Edition fully-compliant application server, like IBM's Websphere, or JBoss (Enterprise Application PlatformWildFly)

Extract contents of a JAR file

This extracts the contents of JAR file (/path/to/file.jar) to current directory. The original JAR file is unchanged.

$ jar xvf /path/to/file.jar

 

List files in JAR file without extracting them

This lists in the files in a JAR file (/path/to/file.jar) without modifying the original JAR file.

$ jar tvf /path/to/file.jar

 

Extract specific files from a JAR file

The following will extract file1_in_jar.txt from the JAR file (path/to/file.jar). You can specify multiple files to extract.

$ jar xvf /path/to/file.jar file1_in_jar.txt

Extract contents of a JAR file without `jar` command line tool (using `unzip`)

JAR files are 'zip' files but with a different extension name. You can use any program that can read 'zip' files to extract them, e.g., `unzip`

$ unzip /path/to/file.jar

NOTE: You may need to rename the file to end with ‘.zip’ for some programs to extract them.

 

List files in JAR file without extracting them without `jar` command line tool (using `unzip`)

This uses `unzip` to list files in a JAR file without modifying the original JAR file (/path/to/file.jar).

$ unzip -l /path/to/file.jar

Extracting contents of a JAR file to specific directory

This uses `unzip` to extract the contents of a JAR file (/path/to/file.jar) into a specified directory (target_directory/)

$ unzip /path/to/file.jar -d target_directory/

Conclusion

JAR files are 'zip' files, so any program that can read 'zip' files can also read JARs. In some cases, you may need to rename the JAR file to end with “.zip” for certain tools that can read 'zip' files to work.

You might also like other posts...