Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How can app read its own Manifest.mf file ? (http://www.velocityreviews.com/forums/t390664-how-can-app-read-its-own-manifest-mf-file.html)

swebb99@gmail.com 02-07-2007 12:43 AM

How can app read its own Manifest.mf file ?
 
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 02-07-2007 04:28 AM

Re: How can app read its own Manifest.mf file ?
 
swebb99@gmail.com 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.

swebb99@gmail.com 02-07-2007 12:00 PM

Re: How can app read its own Manifest.mf file ?
 

Alan Krueger wrote:
> swebb99@gmail.com 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



All times are GMT. The time now is 02:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57