views:

302

answers:

3

Can I access the values defined in a java manifest from code?

+3  A: 

Open the file with JarFile and then call getManifest() to get the Manifest. After that you can access the attributes appropriately.

Jon Skeet
THis is even more flexible. Thank you!
er4z0r
+4  A: 

Many of the values in the MANIFEST.MF can be accessed programmatically without having to find and/or open the jar file itself.

The class java.lang.Package provides access to the ImplementationTitle, ImplementationVendor, ImplementationVersion, SpecificationTitle, SpecificationVendor and the SpecificationVersion.

Information about signed classes can be found using the CodeSource class, which can be retrieved via Class.getProtectionDomain().getCodeSource()

Joachim Sauer
Thanks! Do you happen to know if thats also available to android?
er4z0r
Personally I don't know, but it seems like it: http://developer.android.com/reference/java/lang/package-summary.html
Joachim Sauer
Thanks. I checked it again and found that Jon's solution provides more flexiblity and is available on android as well.
er4z0r
+2  A: 

Here's a simple example of reading the main attributes from a JAR's manifest in situ. It's handy for checking up on what's actually there.

trashgod
+1 for this pretty example
stacker
And +1 from me :-)
er4z0r