Printable Version Printable Version

Reading a Manifest file from Jar file

May 13th, 2009 | By admin | Category: Java

When a JAR file is created, it automatically bundles a default manifest file. There can be only one manifest file in a jar archive, and it always has the path name META-INF/MANIFEST.MF. This post describes , how to read the Manifest.MF file from a jar file. A jar file can support huge range of functionality like , version control , main class defination , electronic signing etc. All these functionality can be achieved through Manifest.MF. A Jar builder post all the required information about the Jar in Manifest.MF file and an application developer would read the Manifest.MF to retrieve the information and work on it.

A simple Manifest.MF would look like this ,


Manifest-Version: 1.0
Main-Class: com.etechguide.java.feature.IdentityNeeded
Name: etechguide/Manifest/
Sealed: true
Specification-Title: "Teaching manifest"
Specification-Version: "1.1"
Implementation-Vendor: "http://etechGuide.in"

Lets see how to read some of these properties at runtime and do something useful.
Let us assume we have a jar file named etechguide.jar which has a Manifest.MF file with following specifications,


Manifest-Version: 1.0
Main-Class: com.etechguide.expert.IdentityNeeded
Implementation-Vendor: "http://etechGuide.in"

Manifest-Version is the Version of the application given as a Jar. Main-Class is the entry point to the application. It should have a full qualified class name and the class should have a public static void main(String []args) method.Implementation-Vendor is the the application/jar provider.
Say ,com.etechguide.expert.IdentityNeeded Class look like as ,

package com.etechguide.expert

public class IdentityNeeded {
public void someMethod(){
System.out.println("I do not do anything.");
}
public static void main(String args[]){
System.out.println("I need Identity");
}
}

Our intention is to read the Manifest.MF file from the etechGuide.jar file and instantiate the Main-class , i.e , com.etechguide.expert.IdentityNeeded and call it’s main method.
Lets read the manifest file first,

public static final String MANIFEST = "META-INF/MANIFEST.MF";
public static final String MAIN_CLASS = "Main-Class";

public String readManifest(JarFile jar) throws IOException {
String mainClass = null;
ZipEntry zipEntry = jar.getEntry(MANIFEST);
if (zipEntry == null) {
// Throw some customized exception here
}
InputStream inputStream = jar.getInputStream(zipEntry);
BufferedReader reader = null;
String key = null;
String value = null;
String lineReader = null;
int inx;
reader = new BufferedReader(new InputStreamReader(inputStream));
// Parsing the Manifest information

while ((lineReader = reader.readLine()) != null && lineReader.length() > 0) {
if ((inx = lineReader.indexOf(":")) > 0) {
key = lineReader.substring(0, inx);
value = lineReader.substring(inx + 1).trim();
} else if (lineReader.charAt(0) == ' ') {
value += lineReader.substring(1);
} else {
key = value = "";
continue;
}
if (key.equalsIgnoreCase(MAIN_CLASS)) {
mainClass = value;
break;
}
}
inputStream.close();
reader.close();
jar.close();

return mainClass;
}

The above method takes a JarFile as an input parameter and parse the Manifest.MF file to return the mainClass which is
com.etechguide.expert.IdentityNeeded in this example.
Now you can instantiate the class to call it’s methods as ,

Class cls = Class.forName(mainClass);
IdentityNeeded identityNeeded = (IdentityNeeded) cls.newInstance();
identityNeeded.main(null); // This would call your main method
identityNeeded.someMethod(); // Some other public method can be accessed in this way.
  • Share/Save/Bookmark
Tags: ,

Leave Comment