Can I access the values defined in a 开发者_如何学JAVAjava manifest from code?
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()
Open the file with JarFile
and then call getManifest()
to get the Manifest
. After that you can access the attributes appropriately.
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.
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();
}
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
精彩评论