jar

List entries of JAR file Manifest

In this example we shall show you how to list entries of a JAR file Manifest. The manifest is a special file that contains information about the files packaged in a JAR file. To list the entries of a JAR file Manifest one should perform the following steps:

  • Create a new JarFile to read from the specified file.
  • Get the Manifest for that file, using the getManifest() API method of the JarFile.
  • Get the manifest entries, with the getEntries() API method. It returns a Map of String – Attributes key-value pairs.
  • For each entry get all attributes. An Iterator can be used over the String keys of the Map of entries to get the Attributes for each key.
  • Then a second Iterator can be used over the Attributes, to get the Attributes.Name, and getValue(Attributes.Name name) API method of the Attributes to get the value of each Attributes.Name.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.Iterator;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class ListEntriesOfJARFileManifest {
	
	public static void main(String[] args) throws Exception {
		
		// create a new JAR file to read from the specified file
		JarFile jarfile = new JarFile("jsp-api.jar");

	    // get the manifest for that file
	    Manifest manifest = jarfile.getManifest();

	    // get the manifest entries
	    Map<String, Attributes> mfEntries = manifest.getEntries();

	    for (Iterator<String> it = mfEntries.keySet().iterator(); it.hasNext(); ) {
	    	
	
  String entryName = it.next();

	
  // get all attributes for the entry
	
  Attributes attrs = mfEntries.get(entryName);

	
  for (Iterator<Object> it2=attrs.keySet().iterator(); it2.hasNext(); ) {
	

Attributes.Name attrName = (Attributes.Name)it2.next();
	

String attrValue = attrs.getValue(attrName);
	

System.out.println(attrName + ":" + attrValue);
	
  }
	    }
		
	}

}

Output:

Implementation-Vendor:Apache Software Foundation
Specification-Title:Java API for JavaServer Pages
Implementation-Title:javax.servlet.jsp
Implementation-Version:2.2.FR
Specification-Version:2.2
Specification-Vendor:Sun Microsystems, Inc.

 
This was an example of how to list entries of a JAR file Manifest in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Varun Khulbey
Varun Khulbey
5 years ago

Map mfEntries = manifest.getEntries();
This line is giving me empty map. can anyone help ?

Here is my code:

import java.io.*;
import java.util.Map;
import java.util.jar.*;

public class Demo {
public static void main(String[] args) throws IOException {
String manifest_destination_filePath = “D:\\lib3\\MANIFEST.MF”;
BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(manifest_destination_filePath)));
Manifest mf = new Manifest(is);
Map mfEntries = mf.getEntries();
System.out.println();

}
}

Back to top button