jar

Create Manifest for JAR File

With this example we are going to demonstrate how to create a Manifest for a JAR File. In short, to create a Manifest for a JAR File you should:

  • Create a FileInputStream by opening a connection to an actual file.
  • Create a new Manifest from the specified input stream.
  • Invoke the getMainAttributes() API method of the Manifest to get the Attributes of the Manifest.
  • Get the Collection view of the attribute name-value mappings contained in the Attributes, with the entrySet() API method of the Attributes.
  • Use an Iterator to get the entries of the Attributes.

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

package com.javacodegeeks.snippets.core;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public class CreateManifestForJARFile {
	
	public static void main(String[] args) throws Exception {
		
		// Create a manifest from a file
	    InputStream fis = new FileInputStream("Manifest.MF");
	    
	    Manifest manifest = new Manifest(fis);
	    
	    Attributes attrs = manifest.getMainAttributes();
	    
	    Set<Map.Entry<Object,Object>> set = attrs.entrySet();
	    
	    Iterator<Map.Entry<Object,Object>> iter = set.iterator();
	    
	    while (iter.hasNext()) {
			Map.Entry<Object, Object> entry = iter.next();
			System.out.println(entry);
		}
		
	}

}

Manifest.MF:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.0
Created-By: 1.6.0_20-b02 (Sun Microsystems Inc.)
X-Compile-Source-JDK: 1.6
X-Compile-Target-JDK: 1.6

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

Output:

Ant-Version=Apache Ant 1.8.0
Manifest-Version=1.0
Created-By=1.6.0_20-b02 (Sun Microsystems Inc.)
X-Compile-Source-JDK=1.6
X-Compile-Target-JDK=1.6

 
This was an example of how to create a Manifest for a JAR File in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He 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.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button