Hi EveryOne,
/*
* 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);
}
}
}
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?
ReplyDeleteI'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?
ReplyDeleteHi Roman,
ReplyDeleteThanks 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.
i am not getting can you tell me on brief....
Deletemy 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...
Thanx for thee clarification. Its really intresting.
ReplyDeletewelcome julu
ReplyDelete