Monday, April 11, 2011

Viewing a JAR's Manifest File

I like the blog post Viewing the MANIFEST.MF from a jar in a single command because it covers a common development environment need many Java developers will run into from time to time and because it covers a nice tip for handling this common need. The post looks specifically at how to use Linux unzip command with its -p option to display the contents of a JAR file's MANIFEST.MF file to sdtout in a single command and without any new files that need to be cleaned up.

Linux unzip -p

The post "Viewing the MANIFEST.MF from a jar in a single command" provides an example of how to use the Linux unzip command and its -p option. I adjust that example slightly for my own example here that I will use in demonstrating several ways to access the contents of the MANIFEST.MF file. For my example, I will be looking at the MANIFEST.MF file contained in the jdiff.jar I have built locally on my system. Although JDiff comes with a pre-built JAR, I have been building and using it locally with Java 7 and thought it provided as good as any JAR upon which to try viewing the manifest file. This JAR's manifest file can be viewed in Linux with the following unzip command:
unzip -p jdiff.jar META-INF/MANIFEST.MF

Because the -p option specifies that the unzip command should write its output to standard output rather than to an actual file, there is no need to clean up any files.

Brute Force jar Approach

The author of the blog post "Viewing the MANIFEST.MF from a jar in a single command" mentions that the "old way" to view a manifest file was often to create a temporary directory, copy the JAR of interest into that temporary directory, change directory to that temporary directory, unzip the JAR file's contents, view the file of interest, and then remove the temporary directory. That post demonstrates the commands that would be done for this approach.

Slightly Less Brute Force jar Approach

Fortunately, the jar command provides a few places to make things a little easier. Specifically, jar allows us to explicitly specify a subset of files we want extracted. This means that instead of making a temporary directory to hold all the extra stuff that we don't want to clutter our current directory, we could simple extract the single file with the jar command and this is often acceptable if it's only one file. This is shown in the next screen snapshot.

The steps shown above aren't too onerous. The single file is extracted with the command
jar xvf jdiff.jar META-INF/MANIFEST.MF
and then all that's left to be done is to run a command like (in DOS)
(cat or less or more could be used in Linux). The only residue left from this is a META-INF directory with the desired MANIFEST.MF file inside of it, but there is still something left to be removed.


The Firefox web browser can render more than simple HTML and text files as is demonstrated in the post View Contents of ZIP/JAR File using Firefox. That post demonstrates a simple example using Firefox 3, but this feature is also available in Firefox 4 as shown in the next three screen snapshots that show opening of the JAR, drilling down into the META-INF directory and then displaying the contents of the MANIFEST.MF file.

The screen snapshots shown above demonstrate how easy it is to see the contents of the JAR in the Firefox web browser! The only "special" treatment of the URL required was to specify jar:file// at the beginning of the URL and to end it with a exclamation point. In my case, it was:
and the more general syntax is:

NetBeans IDE

The Java IDEs are obvious choices for easily seeing the contents of a manifest file. The following screen snapshot demonstrates viewing the manifest file. This was easily brought up in NetBeans by simply using File --> Open File and selecting jdiff.jar to have it displayed as shown in the next screen snapshot.

JDeveloper IDE

Other Java IDEs provide similarly easy-to-use viewing of manifest files within a JAR. The next snapshot demonstrates this for JDeveloper, which allows one to see a JAR file's contents by selecting File --> Open to open the JAR file.

Eclipse IDE

If a JAR is on an Eclipse project's build path, it can be opened up to see the JAR's contents as shown in the next screen snapshot.


When it comes to the tools for Java development, Groovy is never far from my mind as a source for building useful tools. The next code listing shows a simple Groovy script that makes use of Java's JAR-handling classes JarFile, Manifest, and Attributes to print the contents of a given JAR's manifest file to standard output.

#!/usr/bin/env groovy

 * displayManifest.groovy
 * displayManifest <<path_to_jar_file_with_desired_manifest>>
 * This script displays the manifest file of the specified JAR file.
 * This script is written to be very simplistic. It doesn't check to ensure that
 * the provided file is a JAR or ZIP file and, in fact, doesn't even check that
 * a file is passed on the command-line.
new java.util.jar.JarFile(args[0]).manifest.mainAttributes.entrySet().each
   println "${it.key}: ${it.value}"

Most of the above Groovy script is comments. When the comments are removed, it boils down to a very small piece of code. It's true that it lacks any type of usage enforcement to ensure that a JAR file is provided, but it does the job as long as one provides a valid JAR file as the first argument to the script.


This post has demonstrated several different approaches to viewing a JAR's manifest file. Each approach has different advantages and disadvantages. A Java developer's IDE is conveniently available and often can be just what the developer desires. However, if the developer wishes to view the contents of a manifest file outside of the IDE, the are operating-system specific tools like unzip -p that can be used, jar can always be used directly even if brute force is required, and Groovy can be used to write a very simple script that takes advantage of Java's java.util.jar package. Another ubiquitous tool that can be used to view JAR file contents is the Firefox web browser.

1 comment:

@DustinMarx said...

Zemian Deng's post How to print text file content inside a jar file shows another approach using Groovy to display a JAR's MANIFEST.MF file.