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.
Step 1 − Create a folder c:/test/java7/com/howcodex. Create Test.java with following content −
package com.howcodex; public class Tester { public static void main(String[] args) { System.out.println("Inside java 7"); } }
Step 2 − Create a folder c:/test/java9/com/howcodex. Create Test.java with following content −
package com.howcodex; 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/howcodex/Tester.java C:\JAVA > javac --release 7 java7/com/howcodex/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/howcodex/Tester.java, multiple resources with same name
Run with JDK 7
C:\JAVA > java -cp test.jar com.howcodex.Tester Inside Java 7
Run with JDK 9
C:\JAVA > java -cp test.jar com.howcodex.Tester Inside Java 9