Multi-version compatible JARfunction allows us to choose to use the version of the class we want when running the library program in a specific version of the Java environment . We can specify the compiled version through the--releaseparameter.
One specific change is that there is a new attribute in theMANIFEST.MFfile in theMETA-INFdirectory, as shown below
Multi-Release: true
META-INF" directory. If we want to support Java 9 version, there is a 9 directory under the versions directory.
multirelease.jar ├── META-INF │ └── versions │ └── 9 │ └── multirelease │ └── Helper.class ├── multirelease ├── Helper.class └── Main.class
Multi-version compatible JARfunction from "Test.java"The file generates two versions of jar packages. One version isjdk7 and the other version isjdk 9, and then we execute it in different environments.
Step one:Create a folder in theC:/test/java7/com/tutorialspointdirectory and create a "# in the folder ##Test.java" file, as shown below:
package com.tutorialspoint; public class Test { public static void main(String args[]) { System.out.println("Inside Java 7"); } }
Create the folderC:/test/java9/com/tutorialspoint, and create a "Test.java" file in the folder as follows:
package com.tutorialspoint; public class Test { public static void main(String args[]) { System.out.println("Inside Java 9"); } }
We can follow the following Method to compile code:
C:\test> javac --release 9 java9/com/tutorialspoint/Test.java C:\test> javac --release 7 java7/com/tutorialspoint/Test.java
multi-version compatible jar package in the following way
C:\JAVA> jar -c -f test.jar -C java7 . --release 9 -C java9 Warning: entry META-INF/versions/9/com/tutorialspoint/Test.java, multiple resources with same name
C:\JAVA> java -cp test.jar com.tutorialspoint.Test Inside Java 7
##Use JDK 9 to execute:
C:\JAVA> java -cp test.jar com.tutorialspoint.Test Inside Java 9
The above is the detailed content of What is the use of multi-version compatible jars in Java 9?. For more information, please follow other related articles on the PHP Chinese website!