Multi-release JAR files
We have seen many third-party libraries, support for several Java versions with backward compatibility. Because of this, they don't use the latest API features introduced in new releases of JDK. Since Java 8, there is no facility to define condition-based platform dependencies for using new features.
Java 9 introduced a multi-release jar concept. It allows developers to create alternative versions of each class, which are only used when running on specific Java versions.
The preceding diagram shows the structure of a multi-release jar. It contain resources and classes along with the Meta-INF directory for metadata. This metadata file contains version-specific details to encode jar files to provide a compatible method for multiple versions of libraries that run on a target Java platform.
To continue on with the preceding example, we can see that the javaMutipleJar library has three classes, Class1.class, Class2.class, and Class3.class, at root level build with Java 8. If we deploy this jar in JDK, which does not support MARJAR, then only the root level classes will be visible and later platform classes will be ignored.
Moreover, Class1.class and Class2.class want to use Java 9 features, so then only those classes will bundle with Java 9 compilation. When Java 10 comes and Class1.class wants to use Java 10 features, then, as mentioned in the diagram, because of MARJAR concepts, it will bundle with the Java 10 platform.
Ultimately, multi-release jar concepts help third-party library and framework developers to easily separate the use of new APIs for specific JDK for supporting migration as well as continuing to support older versions.