Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

How can app read its own Manifest.mf file ?

Thread view: 
swebb99@gmail.com - 07 Feb 2007 00:43 GMT
Hi,

If I run an application from a jar how do I get a handle on the
standard Manifest file so I can read it.

It appears that there is a JarInputStream that can be used to get hold
of the Manifest Object but I'm not sure how I actually get a handle on
a stream for the correct jar file. I know a class file that is always
in the jar so I assume I can in someway use this to get hold of the
jar file being used and then open a stream to it ????

Any idea's ?

Thanks

Steve
Alan Krueger - 07 Feb 2007 04:28 GMT
> It appears that there is a JarInputStream that can be used to get hold
> of the Manifest Object but I'm not sure how I actually get a handle on
> a stream for the correct jar file. I know a class file that is always
> in the jar so I assume I can in someway use this to get hold of the
> jar file being used and then open a stream to it ????

Take a look at the Class.getProtectionDomain,
ProtectionDomain.getCodeSource, and CodeSource.getLocation methods and
see if those help.
swebb99@gmail.com - 07 Feb 2007 12:00 GMT
> > It appears that there is a JarInputStream that can be used to get hold
> > of the Manifest Object but I'm not sure how I actually get a handle on
[quoted text clipped - 5 lines]
> ProtectionDomain.getCodeSource, and CodeSource.getLocation methods and
> see if those help.

Thanks Alan,

I had a look at some previous load resource code I wrote and it also
used the protection domain. Anyway I ended up using this code which
works for both standalone code from a JAR and WebStart code from a
JAR. Its rough by the way just to see if it works I realise it needs
tweaking ;)

       final ProtectionDomain domain =
agentsupport.class.getProtectionDomain();
       final CodeSource source = domain.getCodeSource();
       URL url = source.getLocation();
       if(url.toExternalForm().endsWith(".jar")) {
           try {
                JarInputStream jarStream = new JarInputStream(url.openStream(),
false);
                Attributes attr = jarStream.getManifest().getMainAttributes();
                Set set = attr.entrySet();
                if(set != null) {
                    log.info("Manifest Attributes :");
                    Iterator it = set.iterator();
                    while(it.hasNext()) {
                        Map.Entry entry = (Map.Entry)it.next();
                        log.info(entry.getKey() + ": " + entry.getValue());
                    }
                }

            } catch (IOException e) {
            }
       }

The only problem I hit was when reading the Manifest from WebStart I
use the Maven 1.1 JNLP plugin and it dumps over the original Manifest
and offers no properties to define what should go in there :( Bugger


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.