Home » Core Java » util » jar » Create Manifest for JAR File

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

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.

0 0 vote
Article Rating

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x