Reputation: 4891
Can I access the values defined in a java manifest from code?
Upvotes: 13
Views: 10349
Reputation: 205775
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. In outline,
var jar = new JarFile(name);
var manifest = jar.getManifest();
var map = manifest.getMainAttributes();
map.entrySet().forEach(e -> {
System.out.println(e.getKey()
+ ": " + e.getValue());
});
Upvotes: 5
Reputation: 105043
Try com.jcabi.manifests.Manifests
utility class from jcabi-manifests. Using this class you can read all available MANIFEST.MF files with one liner:
String name = Manifests.read("Foo-Name");
Also, see this article: http://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html
Upvotes: 0
Reputation: 5720
Use following way to detect External Jar/SDK MANIFEST.MF Info. We could use this info for detecting Jar version etc. Use http://docs.oracle.com/javase/6/docs/api/java/util/jar/Manifest.html
public void getSDKInfo() {
Package pkg = Manifest.class.getPackage();
String specTitle = pkg.getSpecificationTitle();
String vendor = pkg.getSpecificationVendor();
String version = pkg.getSpecificationVersion();
}
Upvotes: 4
Reputation: 308001
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()
Upvotes: 19