YouTip LogoYouTip

Java9 Multirelease Jar

# Java 9 Multi-Release JAR [!(#) Java 9 New Features](#) The multi-release JAR feature allows you to create libraries that select which class version to use based on the specific Java runtime environment. You specify the compilation version using the `--release` parameter. The specific change is the addition of a new attribute in the `MANIFEST.MF` file under the `META-INF` directory: Multi-Release: true Additionally, a new `versions` directory is added under `META-INF`. If you want to support Java 9, there will be 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 a JAR file from `Tester.java`: one for JDK 7 and another for JDK 9. We then execute them in different environments. **Step 1** Create the folder `c:/test/java7/com/` and create a `Test.java` file in it with the following code: ## Example ```java package com.; public class Tester { public static void main(String[] args){ System.out.println("Inside java 7"); } } **Step 2** Create the folder `c:/test/java9/com/` and create a `Test.java` file in it with the following code: ## Example ```java package com.; public class Tester { public static void main(String[] args){ System.out.println("Inside java 9"); } } Compile the source code: ```bash C:test > javac --release 9 java9/com//Tester.java C:JAVA > javac --release 7 java7/com//Tester.java Create the multi-release JAR: ```bash C:JAVA > jar -c -f test.jar -C java7 . --release 9 -C java9 . Warning: entry META-INF/versions/9/com//Tester.java, multiple resources with same name Execute using JDK 7: ```bash C:JAVA > java -cp test.jar com..Tester Inside Java 7 Execute using JDK 9: ```bash C:JAVA > java -cp test.jar com..Tester Inside Java 9 [!(#) Java 9 New Features](#)
← Java9 Stream Api ImprovementsJava9 New Features β†’