Home » Core Java » util » jar » Write JAR file Manifest to 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.

Write JAR file Manifest to file

This is an example of how to write a JAR file Manifest to a file. A JAR file is essentially a zip file that contains an optional META-INF directory. In this directory there is a Manifest, that is used to specify meta-information about the jar file and its entries. Writing a JAR file’s Manifest to a file implies that you should:

  • Create a new JarFile to read from the specified jar file.
  • Get the Manifest for that file, using the getManifest() API method of the JarFile.
  • Write the manifest to a file, using a FileOutputStream and the write(OutputStream out) API method of the Manifest,

as described in the code snippet below.
 

package com.javacodegeeks.snippets.core;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class WriteJARFileManifestToFile {
	
	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();

	    // write the manifest to a file
	    OutputStream fos = new FileOutputStream("Manifest.MF");
	    
	    manifest.write(fos);
	    fos.close();
		
	}

}

Output:

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/
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 write a JAR file Manifest to a file in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of