Extracting and Viewing JAR Contents
You'll learn how to inspect and extract files from JAR archives using command-line tools, and understand the true nature of JAR files.
JARs Are Just ZIP Files
Here's a secret: JAR files are simply ZIP files with a specific structure. They use standard ZIP compression and can be opened with any ZIP tool. The main difference is that JARs follow a convention—they include a META-INF/ directory with a MANIFEST.MF file and organize Java classes according to package structure.
Listing JAR Contents: jar -t
To see what's inside a JAR without extracting anything, use the -t (table of contents) option:
jar -t -f myapp.jar
This lists all files in the archive, showing the directory structure and filenames. It's like peeking through a window before entering a building.
Extracting JAR Contents: jar -x
To extract files from a JAR, use the -x (extract) option:
jar -x -f myapp.jar
This unpacks all files into the current directory, preserving the package/directory structure. You can also extract specific files by naming them:
jar -x -f myapp.jar com/mycompany/MyClass.class
Verbose Mode
Add -v to either command for verbose output showing detailed information as files are processed:
jar -t -v -f myapp.jar
This displays file sizes, modification dates, and other metadata—useful for troubleshooting.
Key Takeaway: JAR files are ZIP files with Java-specific conventions; use jar -t to list contents and jar -x to extract files, understanding that you can even use standard ZIP tools if needed.