How to read MANIFEST.MF file from JAR using Bash
Asked Answered
A

6

111

I need to read MANIFEST.MF maven manifest file from "some.jar" using bash

Arrange answered 15/8, 2011 at 14:23 Comment(1)
jar files are just zip files.Fanchie
A
185
$ unzip -q -c myarchive.jar META-INF/MANIFEST.MF
  • -q will suppress verbose output from the unzip program
  • -c will extract to stdout

Example:

$ unzip -q -c commons-lang-2.4.jar META-INF/MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_13-119 (Apple Inc.)
Package: org.apache.commons.lang
Extension-Name: commons-lang
Specification-Version: 2.4
Specification-Vendor: Apache Software Foundation
Specification-Title: Commons Lang
Implementation-Version: 2.4
Implementation-Vendor: Apache Software Foundation
Implementation-Title: Commons Lang
Implementation-Vendor-Id: org.apache
X-Compile-Source-JDK: 1.3
X-Compile-Target-JDK: 1.2

Alternatively you can use -p instead of -q -c.

-p extract files to pipe (stdout). Nothing but the file data is sent to stdout, and the files are always extracted in binary format, just as they are stored (no conversions).

Armallas answered 15/8, 2011 at 14:33 Comment(1)
I know this thread is old, but for whom it may concern: As from the manual, extracting using -p or -c will print output in binary form. If you need to parse this output somehow (for example to associative array), you should force text representation with -aa argument, to have it correct.Burial
R
25

use unzip:

$ unzip -q -c $JARFILE_PATH META-INF/MANIFEST.MF

that will quietly (-q) read the path META-INF/MANIFEST.MF from the jarfile (which is compressed using the zip format) to stdout (-c). You can then pipe the output to other command to answer questions like 'what is the main class for this jar:

$ unzip -q -c $JARFILE_PATH META-INF/MANIFEST.MF | grep 'Main-Class' | cut -d ':' -f 2

(this removes all lines which don't contain the string Main-Class, then splits the line at :, keeping only the second field, the class name). Of course, either define $JARFILE_PATH appropriately or replace $JARFILE_PATH with the path to a jarfile you're interested in.

Reckford answered 15/8, 2011 at 14:33 Comment(0)
Z
4

Depending on your distribution, install the unzip package. Then simply issue

unzip -p YOUR_FILE.jar META-INF/MANIFEST.MF

This will dump the contents to STDOUT.

HTH

Zebulen answered 15/8, 2011 at 14:33 Comment(0)
M
3

$ tar xfO some.jar META-INF/MANIFEST.MF

x extracts and O redirects to stdout.

Note: Seem to work only in bsdtar, not GNU tar.

Magistery answered 9/6, 2015 at 8:56 Comment(0)
S
1

Others have been posting about using unzip -p and piping to grep or awk or whatever you need. While that works for most cases, it's worth noting that because of the 72 characters-per-line limit of MANIFEST.MF, you may be grepping for keys whose values are split across multiple lines and will therefore be very difficult to parse. I'd love to see a CLI tool that can actually pull a rendered value out of the file.

http://delaltctrl.blogspot.com/2009/11/manifestmf-apparently-you-are-just.html

Spoilfive answered 26/6, 2015 at 22:18 Comment(1)
I added an answer with a Groovy script that uses Java's API to obtain and print the rendered key/value pairs, as well as a bash-friendly one-liner to invoke it.Goodard
G
0

The following Groovy script uses Java's API to parse the manifest, avoiding issues with the manifest format's weird line breaking:

#!/usr/bin/env groovy
for (arg in args) {
  println("[$arg]")
  jarPath = new java.io.File(arg).getAbsolutePath()
  jarURL = new java.net.URL("jar:file:" + jarPath + "!/")
  m = jarURL.openConnection().getManifest()
  m.getMainAttributes().each { k, v -> println("$k = $v") }
}

Pass JAR files as arguments:

$ groovy manifest.groovy ~/.m2/repository/junit/junit/4.13/junit-4.13.jar
[/Users/curtis/.m2/repository/junit/junit/4.13/junit-4.13.jar]
Implementation-Title = JUnit
Automatic-Module-Name = junit
Implementation-Version = 4.13
Archiver-Version = Plexus Archiver
Built-By = marc
Implementation-Vendor-Id = junit
Build-Jdk = 1.6.0_65
Created-By = Apache Maven 3.1.1
Implementation-URL = http://junit.org
Manifest-Version = 1.0
Implementation-Vendor = JUnit

Or if you're desperate for a one-liner:

groovy -e 'new java.net.URL("jar:file:" + new java.io.File(args[0]).getAbsolutePath() + "!/").openConnection().getManifest().getMainAttributes().each { k, v -> println("$k = $v") }' ~/.m2/repository/junit/junit/4.13/junit-4.13.jar
Goodard answered 28/4, 2020 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.