Skip to main content

Getting the Manifest Version inside a Jar File

/**
*
*/



import java.io.File;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.Logger;


/**
* @author Kiran
*
*/


public class JarFileVersionInfoNew {

/**
* A logger for this class.
**/
private static final Logger LOG =
Logger.getLogger(JarFileVersionInfoNew.class.getName());



/**
* Displays the attributes contained in the manifest of one (or
* several jar files...
*
* @param args a list of jar file.
*/
public static void main(String[] args) {
final String path = "C:/Documents and Settings/kkanapar/Workspaces/WSRP/liferay-portal-new/lib/global";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
System.out.println("Number of files and directories in the file system You gave: " + listOfFiles.length);
for (int i = 0; i < listOfFiles.length; i++) {
String tempFileName = null;
if (listOfFiles[i].isFile()) {
tempFileName = listOfFiles[i].getName();
System.out.println("File " +"-"+i+": "+ tempFileName);
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " +"-"+i+": "+ listOfFiles[i].getName());
}



final File f = new File(path+"\\"+tempFileName);
if (!f.canRead()) {
System.err.println(f.getAbsolutePath()+": no such file");
continue;
}
try {
JarFile jar = new JarFile(f);
final Manifest manifest = jar.getManifest();
final Attributes mattr = manifest.getMainAttributes();
System.out.println(f.getAbsolutePath());
System.out.println("Main attrs: ");
for (Object a : mattr.keySet()) {
System.out.println("\t "+a+": "+mattr.getValue((Name)a));
}


System.out.println("\nReading other attrs:\n");

final Map attrs = manifest.getEntries();
for (String name : attrs.keySet()) {
final Attributes attr = attrs.get(name);
System.out.println(name+": \n");
for (Object a : attr.keySet()) {
System.out.println("\t "+a+": "+attr.getValue((Name)a));
}
}
System.out.println("\n ======================================================================================\n");

} catch (Exception x) {
System.err.println("Failed to read manifest for "+
f.getAbsolutePath()+": "+x);
}
}
}

}

Comments

Popular posts from this blog

Design Patterns using Java

According to Gang of Four(GOF) any software is classified in to one of the three categories. I read so many books about design patterns which provide a lot of information about Design Patterns in a language neutral way or related to a particular programming language. I am trying to complement the great books by providing the precise and concise information that is required in the day to day programming of a Java Developer. Any software can be classified into one of the three categories -Framework, Toolkit, Application. Framework - Framework defines a set of steps to create an application. Framework provides design reuse. Toolkit - Toolkit provides some utility functions to an existing application. Toolkit provides code reuse. Application - Application is some thing that is specific to the project, and is not useful outside the context of the current application. Gang of Four divided the Design Patterns in to 3 types based on their usage. There are 3 types of Gang of Fo