English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Other Java topics

Java 9 Multi-version compatibility jar package

Java 9 New Features

The multi-version compatibility JAR feature allows you to create library programs that only use the class version for specific versions of the Java environment.

Through --release parameter specifies the compilation version.

The specific changes are that META-INF directory added a property to the MANIFEST.MF file:

Multi-Release: true

Then META-INF directory also added a versions directory, if you want to support java9Then there are 9 directory.

multirelease.jar
├── META-INF
│   └── versions
│       └── 9
│           └── multirelease
│               └── Helper.class
├── multirelease
    ├── Helper.class
    └── Main.class

In the following example, we use the multi-version compatibility JAR feature to generate two versions of jar packages for the Tester.java file, one is jdk 7Another one is jdk 9Then we execute in different environments.

First step

Create folder c:/test/java7/com/w3Create a codebox and create a Test.java file in the folder, the code is as follows:

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

Second step

Create folder c:/test/java9/com/w3Create a codebox and create a Test.java file in the folder, the code is as follows:

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

Compile source code:

C:\test > javac --release 9 java9/com/w3codebox/Tester.java
C:\JAVA > javac --release 7 java7/com/w3codebox/Tester.java

Create 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/w3codebox/Tester.java, 
   multiple resources with same name

Using JDK 7 Execution:

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

Using JDK 9 Execution:

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

Java 9 New Features