Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java 9 Multi-Release JAR Files

Java 9 Multi-Release JAR Files

Java 9 New Features Java 9 New Features

The multi-release JAR feature lets you create class versions that are selected for use only when the library runs in a specific version of the Java environment.

Specify the compilation version with the --release parameter.

The specific change is that a new attribute is added to the MANIFEST.MF file in the META-INF directory:

Multi-Release: true

Also, a versions directory is added under META-INF; if java9 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 build two versions of the jar from Tester.java, one for jdk 7 and one for jdk 9, and then run them in different environments.

Step 1

Create the folder c:/test/java7/com/runoob, and create the Test.java file in it, with the following code:

Example

package

com.runoob

;

public

class

Tester

{

   

public

static

void

main

(

String

[

]

args

)

{

     

System

.

out

.

println

(

"Inside java 7"

)

;

   

}

}

Step 2

Create the folder c:/test/java9/com/runoob, and create the Test.java file in it, with the following code:

Example

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 the multi-release JAR

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

Run with JDK 7:

C:\JAVA > java -cp test.jar com.runoob.Tester

Inside Java 7

Run with JDK 9:

C:\JAVA > java -cp test.jar com.runoob.Tester

Inside Java 9

Java 9 New Features Java 9 New Features