Listing the Main Attributes in a JAR File Manifest

Jar Manifest: Jar Manifest file is the main
section of a jar file. This file contains the detailed information about the
specific individual jar file. It contains the complete information about the jar
file, each and every section or
particular information is separated by new line. For mentioning information in
the manifest file you have to maintain the information syntax with rule and
restrictions.
Main Attributes in Jar file are these attributes which
are applied for each and every individual manifest file for entry. This section,
in which these attributes are defined, contains jar configuration and security
information. This section is terminated by new empty line.
An individual section of the manifest file contains the
list of the information about packages and files which are to be signed. There
not need to list all the files of the jar file. The manifest file itself not nee
to be listed. Every section starts with the name of attributes and values of
these attributes. Attributes and values are separated with colon
(":"). Values of the attributes are mentioned according to the
attributes like relative path to the path, an absolute URL for archiving data
from outside.
Program Result:
Following program is given for the best illustration of
the jar manifest file. This program takes a jar file which manifest file content
has to be shown and helping you to show the main attributes and values of all
attributes mentioned in the manifest file. It checks for the file format (for
the jar format) and it also checks whether the file exists or not.
Here is the code of the program:
import java.util.*;
import java.util.jar.*;
import java.io.*;
public class MainJarAtr{
public static void main(String[] args){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter jar file name: ");
String filename = in.readLine();
if(!filename.endsWith(".jar")){
System.out.println("File not in jar format.");
System.exit(0);
}
File file = new File(filename);
if (file.exists()){
// Open the JAR file
JarFile jarfile = new JarFile(filename);
// Get the manifest
Manifest manifest = jarfile.getManifest();
// Get the main attributes in the manifest
Attributes attrs = (Attributes)manifest.getMainAttributes();
// Enumerate each attribute
for (Iterator it=attrs.keySet().iterator(); it.hasNext(); ) {
// Get attribute name
Attributes.Name attrName = (Attributes.Name)it.next();
System.out.print(attrName + ": ");
// Get attribute value
String attrValue = attrs.getValue(attrName);
System.out.print(attrValue);
System.out.println();
}
}
else{
System.out.print("File not found.");
System.exit(0);
}
}
catch (IOException e) {}
}
}
|
Download this example.
 |