jar

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.

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