13

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

er4z0r
  • 4,711
  • 8
  • 42
  • 62

5 Answers5

19

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
  • 302,674
  • 57
  • 556
  • 614
11

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

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();
   }
David Victor
  • 830
  • 9
  • 29
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
4

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
  • 203,806
  • 29
  • 246
  • 1,045
0

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

yegor256
  • 102,010
  • 123
  • 446
  • 597