Specifying a version for a Java package

You can specify specification and implementation information through the manifest file inside the jar file in which you package up your package. You can specify the following:

   Specification-Title
   Specification-Vendor
   Specification-Version
   Implementation-Title
   Implementation-Vendor
   Implementation-Version

Note that Specification-Version and Implementation-Version need to be encoded as numbers separated by dots, eg. 1.2.3 This is important to compare versions and to determine compatibility.

Following example shows you how to add versioning to a package of your own:

1) Start by writing your package:

mypackageMain.java:

package mypackage;
 
public class Main {
   public static void main(String args[]) {
      Package p = Package.getPackage("mypackage");
 
      System.out.println("For package: " + p.getName());
      System.out.println("  Specification-Title: " + 
         p.getSpecificationTitle());
      System.out.println("  Specification-Vendor: " + 
         p.getSpecificationVendor());
      System.out.println("  Specification-Version: " + 
         p.getSpecificationVersion());
      System.out.println("  Implementation-Title: " + 
         p.getImplementationTitle());
      System.out.println("  Implementation-Vendor: " + 
         p.getImplementationVendor());
      System.out.println("  Implementation-Version: " + 
         p.getImplementationVersion());
   }
}

2) write a manifest file:

Manifest.mf:

Manifest-Version: 1.0
Name: mypackage
Specification-Title: mypackage Specification
Specification-Vendor: Esus, Inc.
Specification-Version: 1.0
Implementation-Title: mypackage implementation
Implementation-Vendor: Esus, Inc.
Implementation-Version: 1.0
Main-Class: mypackage/Main
 

3) package everything up in a JAR file:

jar cfm Main.jar Manifest.mf mypackage/*.class

4) as we have included the Main-Class attribute in the manifest file, we can run it as follows:

java -jar Main.jar

outputs:

For package: mypackage
  Specification-Title: mypackage Specification
  Specification-Vendor: Esus, Inc.
  Specification-Version: 1.0
  Implementation-Title: mypackage implementation
  Implementation-Vendor: Esus, Inc.
  Implementation-Version: 1.0