logo

Multirelease JAR


Show

A new feature is introduced in java 9 in which a JAR format has been increased to have different versions of the java class and resources can be maintained and used as per the platform. In JAR a MANIFEST.MF file has an entry Multi-Release: accurate in its important section. META-INF directory also has aversions subdirectory whose subdirectories (starting with 9 for Java 9 ) store version-specific classes and resource files.

We will be using a multi-release jar to get two versions of the Tester.java file in this example and one for JDK 7 and another one for JDK 9 and run it on a distinct JDK version.

Steps

Step-1 Make a folder c:/test/java7/com/intellinuts. Create Test.java with given content

Tester.java

package com.intellinuts.greetings;

public class Tester {
   public static void main(String[] args) {
      System.out.println("Inside java 7");
   }
}

Step-2 Make a folder c:/test/java9/com/intellinuts. Create Test.java with the given content

Tester.java

package com.intellinuts.greetings;

public class Tester {
   public static void main(String[] args) {
      System.out.println("Inside java 9");
   }
}

Assemble the source code

C:\test > javac --release 9 java9/com/intellinuts/Tester.java
C:\JAVA > javac --release 7 java7/com/intellinuts/Tester.java

Create a Multi-Release jar

C:\JAVA > jar -c -f test.jar -C java7 . --release 9 -C java9.
Warning: entry META-INF/versions/9/com/intellinuts/Tester.java, 
   multiple resources with same name

Now, Run with JDK 7

C:\JAVA > java -cp test.jar com.intellinuts.greetings.Tester
Inside Java 7

Now, Run with JDK 9

C:\JAVA > java -cp test.jar com.intellinuts.greetings.Tester
Inside Java 9