The multi-version compatible JAR feature allows you to create a class version that you choose to use only when running the library program in a specific version of the Java environment.
Specify the build version with the --release parameter.
The specific change is the addition of a new attribute to the MANIFEST.MF file in the META-INF directory:
Multi-Release: true
Then a new versions directory is added under the META-INF directory. If it is to support java9, there are 9 directories in 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-version compatible JAR function to generate two versions of the jar package Tester.java file, one is jdk 7, the other is jdk 9, and then we execute in different environments.
first step
Create a folder c:/test/java7/com/runoob and create a Test.java file in this folder. The code is as follows:
package com.runoob; public class Tester { public static void main(String[] args) { System.out.println("Inside java 7"); } }
Second step
Create a folder c:/test/java9/com/runoob and create a Test.java file in this folder. The code is as follows:
package com.runoob; 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/runoob/Tester.java C:\JAVA > javac --release 7 java7/com/runoob/Tester.java
Create a multi-version compatible jar package
C:\JAVA > jar -c -f test.jar -C java7 . --release 9 -C java9. Warning: entry META-INF/versions/9/com/runoob/Tester.java, multiple resources with same name
Use JDK 7 to execute:
C:\JAVA > java -cp test.jar com.tutorialspoint.Tester Inside Java 7
Use JDK 9 to execute:
C:\JAVA > java -cp test.jar com.tutorialspoint.Tester Inside Java 9
Comments
Post a Comment