Friday, March 6, 2009

Reading custom field from Manifest file in JAVA

Hi EveryOne,


If your PM tell you declare a version number for your application.
And if you declared application version number as constant in source code.

Then what happens when your application update and version number should be change?

What will you do, again change in source code, then compile, then create jar etc?

It is not just a case where your constant need to update.

i have a solution.

Just add a field on manifest file. And read these fields from your code.
Here i am putting a example code to do so.

It is very easy. You can read all fields from manifest.

For source code see below.

/*

* Program for reading a field from manifest file

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

* */

import java.io.*;

import java.util.*;

import java.util.jar.*;

import java.net.*;

public class JarRead

{

public static void main(String[] args) throws Exception

{

new JarRead().call();

}

public void call()

{

try

{

////pass your jar file name which included manifest file

JarFile jf = new JarFile("CDViewer.jar");

////getting manifest file from jar file

Manifest m = jf.getManifest();

////getting all attribute from manifest file

Attributes attr = m.getMainAttributes();

////taking values from manifest file.

////existing field

String SOFTWARE_VERSION = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION); // 2.0

String SOFTWARE_VENDOR = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR); // YuvaDevelopers Pvt.Ltd.

String SOFTWARE_VENDOR_TITLE = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE); // ManifestReader

////custom defined field

String dType = attr.getValue("RunType");

System.out.println(SOFTWARE_VERSION + "\n" + SOFTWARE_VENDOR + "\n" + SOFTWARE_VENDOR_TITLE + "\n" + dType);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

Manifest file:-

Manifest-Version: 1.2
Main-Class: JarRead
Created-By: 1.4 (Sun Microsystems Inc.)
Specification-Title: build57
Specification-Version: 1.0
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: Manifest Reader
Implementation-Version: 1.0.0
Implementation-Vendor: YuvaDevlopers Pvt.Ltd.
RunType: Customfield

Just save as manifest.mf file and add while creating jar file.

6 comments:

  1. I'm not sure is very interesting to read from the manifest, however some thoughts and examples of java. Source code helps us to investigate some other issues of getting resources from the Jar file itself. Especially playing with classloaders a pitfall lies off underneath to explore the availability of resources. The same thing could happen with any resource like manifest.mf. My jar file needs some system property set in VM to run. Can you put a system property to the manifest other than Class-Path? Whose reading that values – nobody but you and you can’t get worth of it?

    ReplyDelete
  2. I'm not sure is very interesting to read from the manifest, however some thoughts and examples of java. Source code helps us to investigate some other issues of getting resources from the Jar file itself. Especially playing with classloaders a pitfall lies off underneath to explore the availability of resources. The same thing could happen with any resource like manifest.mf. My jar file needs some system property set in VM to run. Can you put a system property to the manifest other than Class-Path? Whose reading that values – nobody but you and you can’t get worth of it?

    ReplyDelete
  3. Hi Roman,
    Thanks for most asking question?

    Yes off course you can do it,But not directly.
    See how.

    Add a manifest field what ever the name because its a user defined.
    Say in my case i am adding a field name "MyCommand"

    put your system property value on that.

    Manifest.mf
    MyCommand: -Djava.security.auth.login.config=theconfig.config

    Hope you get my point.
    Now read this field same as in my above example
    but just change the RunType field name to MyCommand.

    String dType = attr.getValue("RunType");

    Now from your java code split this string into to parts from "=" sign and pass both string on below method.

    System.setProperty("java.security.auth.login.config","theconfig.config");

    Now it is working for me.
    Because you can add any custom field there so no need to worry what would be the field name.
    Its according to you.

    Hope it will work for you too.
    Thanks for commenting.
    See if you have any more problem.

    ReplyDelete
    Replies
    1. i am not getting can you tell me on brief....
      my class name is manifestClass so jar is made as manifestClass.jar and i am passing inside jarfile jf=new jarfile("manifestClass.jar") but it is showing file not found exeception....help me...

      Delete
  4. Thanx for thee clarification. Its really intresting.

    ReplyDelete