Get versioning information about a Java package

The following example creates a JAR file containing one class in the package com.esus.test with extra versioning information in the manifest file. The Main program prints out this info.

com/esus/test/TestClass.java:

package com.esus.test;
 
public class TestClass
{
   public void testMethod() {
      System.out.println("Hello world!");
   }
}

MANIFEST.MF:

Manifest-Version: 1.0
  
Name: com/esus/test/
Specification-Title: Test Specification
Specification-Vendor: Esus, Inc.
Specification-Version: 1.0
Implementation-Vendor: Esus, Inc.
Implementation-Title: Test Reference Implementation
Implementation-Version: 1.5

Note that you can specify this versioning information not only on package level (com/esus/test) but also on the top of the file. If no version information is found about a specific package, this general version information is returned.

To create this JAR file:

C:\myjars>dir
 Volume in drive C has no label.
 Volume Serial Number is 3C7A-79F5
 
 Directory of C:myjars
 
01/19/2003  06:55 PM    <DIR>          .
01/19/2003  06:55 PM    <DIR>          ..
01/19/2003  05:13 PM    <DIR>          com
01/19/2003  06:41 PM               268 MANIFEST.MF
               1 File(s)            268 bytes
               3 Dir(s)  34,651,611,136 bytes free
  
C:\myjars>jar cfm test.jar MANIFEST.MF com

Add test.jar to your classpath and run the following program to retrieve version information.

Main.java:

import com.esus.test.*;
 
public class Main
{
   public static void main(String []args) {
      Package pckg = TestClass.class.getPackage();
 
      System.out.println(pckg.getSpecificationTitle());
      System.out.println(pckg.getSpecificationVendor());
      System.out.println(pckg.getSpecificationVersion());
      System.out.println(pckg.getImplementationTitle());
      System.out.println(pckg.getImplementationVendor());
      System.out.println(pckg.getImplementationVersion());
   }
}

outputs:

Test Specification
Esus, Inc.
1.0
Test Reference Implementation
Esus, Inc.
1.5