How can app read its own Manifest.mf file ?

Discussion in 'Java' started by swebb99@gmail.com, Feb 7, 2007.

  1. Guest

    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
     
    , Feb 7, 2007
    #1
    1. Advertising

  2. Alan Krueger Guest

    wrote:
    > 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.
     
    Alan Krueger, Feb 7, 2007
    #2
    1. Advertising

  3. Guest

    Alan Krueger wrote:
    > wrote:
    > > 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.


    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
     
    , Feb 7, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    880
    Roedy Green
    Sep 26, 2005
  2. Saverio M.
    Replies:
    0
    Views:
    521
    Saverio M.
    Jul 3, 2006
  3. thunk
    Replies:
    1
    Views:
    318
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    491
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    629
    thunk
    Apr 3, 2010
Loading...

Share This Page