Java 9 Multi-Release JAR Files
The multi-release JAR feature allows you to create class versions that are only selected when running library programs in a specific version of the Java environment.
via--releasethe parameter to specify the compilation version.
The specific change is that a new attribute has been added to the MANIFEST.MF file in the META-INF directory:
Multi-Release: true
In addition, a versions directory is added under the META-INF directory. If Java 9 is to be supported, there is a 9 directory under the versions directory.
multirelease.jar
├── META-INF
│ └── versions
│ └── 9
│ └── multirelease
│ └── Helper.class
├── multirelease
├── Helper.class
└── Main.class
In the following example, we use the multi-release JAR feature to generate two versions of the jar package from the Tester.java file, one for JDK 7 and the other for JDK 9, and then we execute them in different environments.
Step 1
Create the folder c:/test/java7/com/example, and create the Test.java file in this folder. The code is as follows:
Example
public class Tester {
public static void main(String[] args) {
System.out.println("Inside java 7");
}
}
Step 2
Create the folder c:/test/java9/com/example, and create the Test.java file in this folder. The code is as follows:
Example
public class Tester {
public static void main(String[] args) {
System.out.println("Inside java 9");
}
}
Compile the source code:
C:\test > javac --release 9 java9/com/example/Tester.java C:\JAVA > javac --release 7 java7/com/example/Tester.java
Create a multi-release JAR file
C:\JAVA > jar -c -f test.jar -C java7 . --release 9 -C java9 . Warning: entry META-INF/versions/9/com/example/Tester.java, multiple resources with same name
Execute using JDK 7:
C:\JAVA > java -cp test.jar com.example.Tester Inside Java 7
Execute using JDK 9:
C:\JAVA > java -cp test.jar com.example.Tester Inside Java 9Other extensions
Java 9 New Features