Java 9 – Multirelease JAR

  • Post author:
  • Post category:Java 9
  • Post comments:1 Comment
Java 9 - Multirelease JAR

This topic is about Java 9 – Multirelease JAR.

In java 9, a new feature is introduced where a jar format has been enhanced to have different versions of java class or resources can be maintained and used as per the platform. In JAR, a file MANIFEST.MF file has a entry Multi-Release: true in its main section. META-INF directory also contains a versions subdirectory whose subdirectories (starting with 9 for Java 9 ) store version-specific classes and resource files.

In this example, we’ll be using a multi-release jar to have two versions of Tester.java file, one for jdk 7 and one for jdk 9 and run it on different jdk versions.

Steps

Step 1 − Create a folder c:/test/java7/com/Adglob. Create Test.java with following content −

Tester.java

package com.Adglob;

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

Step 2 − Create a folder c:/test/java9/com/Adglob. Create Test.java with following content −

Tester.java

package com.Adglob;

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

Compile the source codes.

C:\test > javac --release 9 java9/com/Adglob/Tester.java

C:\JAVA > javac --release 7 java7/com/Adglob/Tester.java

Create the multi-release jar

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

Run with JDK 7

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

Run with JDK 9

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

In this topic we learend about Java 9 – Multirelease JAR. To know more, Click Here.

This Post Has One Comment

Leave a Reply