JAVA9 new Features-Multiple version compatible jar package

Source: Internet
Author: User

1. Official feature

238:multi-release JAR Files

2. Instructions for use

When a new version of Java appears, it takes a few years for your library user to switch to this new version. This means that the library has to be backwards compatible with the oldest Java version you want to support (in many cases Java 6 or JAVA7). This actually means that for a long time to come, you can't use the new features provided by Java 9 in the library. Fortunately, the multi-version compatible Jar feature allows you to create a class version that you can choose to use only in a specific version of the Java environment where the library program is run.

Example 1:

Jar Root

-A.class

-B.class

-C.class

-D.class

-Meta-inf

-Versions

-9

-A.class

-B.class

Description

In the above scenario, Root.jar can be used in Java 9, but the A or class B uses two classes that are not the top-level root.a or root.b, but are in the two below "META-INF/VERSIONS/9". This is a class version specially prepared for JAVA9 and can be used with the features and libraries provided by Java 9. It is also possible to use this jar in earlier versions of Java, because older versions of Java only see the top Class A or class B.

Example 2:

Jar Root

-A.class

-B.class

-C.class

-D.class

-Meta-inf

-Versions

-9

-A.class

-B.class

-10

           -

A.class

Official Note:

By this scheme, it's possiblefor versions of a class designed for a later Java platform release to overridethe version of That's same class designed for a earlier Java platform release.

3. Examples of Use

Step one: Provide the necessary classes

The following classes are available under the specified directory (E:\TEACH\01_JAVA9\MULTIJAR\SRC\MAIN\JAVA\COM\ATGUIGU):

"Generator.java"

"Application.java"

A class with the same name is provided under the following directory (E:\teach\01_Java9\multijar\src\main\java-9\com\atguigu):

"Generator.java"

Step Two: Pack

The instructions are as follows:

javac-d Build--release 8 Src/main/java/com/atguigu/*.java

javac-d build9--release 9 Src/main/java-9/com/atguigu/*.java

Jar--create--main-class=application--file multijar.jar-c build. --release 9-c build9.

Step three: Test in Java 9 and prior versions of the environment



Still Silicon Valley interviewer Song Hongkang
Links: https://www.jianshu.com/p/972c7d531094
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

JAVA9 new Features-Multiple version compatible jar package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.