
- Java 9 Tutorial
- Java 9 - Home
- Java 9 - Overview
- Java 9 - Environment Setup
- Java 9 - Module System
- Java 9 - REPL (JShell)
- Java 9 - Improved JavaDocs
- Java 9 - Multirelease JAR
- Java 9 - Collection Factory Methods
- Java 9 - Private Interface Methods
- Java 9 - Process API Improvements
- Java 9 - Stream API Improvements
- Try With Resources improvement
- Enhanced @Deprecated Annotation
- Inner Class Diamond Operator
- Optional Class Improvements
- Java 9 - Multiresolution Image API
- CompletableFuture API Improvements
- Java 9 - Miscellaneous Features
- java9 Useful Resources
- Java 9 - Questions and Answers
- Java 9 - Quick Guide
- Java 9 - Useful Resources
- Java 9 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 9 - Multirelease JAR
In java 9, a new feature is introduced where a jar format has been enhanced to have different versions of java class or resources can be maintained and used as per the platform. In JAR, a file MANIFEST.MF file has a entry Multi-Release: true in its main section. META-INF directory also contains a versions subdirectory whose subdirectories (starting with 9 for Java 9 ) store version-specific classes and resource files.
In this example, we'll be using a multi-release jar to have two versions of Tester.java file, one for jdk 7 and one for jdk 9 and run it on different jdk versions.
Steps
Step 1 − Create a folder c:/test/java7/com/tutorialspoint. Create Test.java with following content −
Tester.java
package com.tutorialspoint; public class Tester { public static void main(String[] args) { System.out.println("Inside java 7"); } }
Step 2 − Create a folder c:/test/java9/com/tutorialspoint. Create Test.java with following content −
Tester.java
package com.tutorialspoint; public class Tester { public static void main(String[] args) { System.out.println("Inside java 9"); } }
Compile the source codes.
C:\test > javac --release 9 java9/com/tutorialspoint/Tester.java C:\JAVA > javac --release 7 java7/com/tutorialspoint/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/tutorialspoint/Tester.java, multiple resources with same name
Run with JDK 7
C:\JAVA > java -cp test.jar com.tutorialspoint.Tester Inside Java 7
Run with JDK 9
C:\JAVA > java -cp test.jar com.tutorialspoint.Tester Inside Java 9