The third part of the Java9 series-the same Jar supports multiple JDK versions to run

I plan to write a series of articles about Java 9 in the next period of time. Although Java 9 is not a core Java version like Java 8 or Java 11, there are still many features worth paying attention to. Looking forward to your attention, I will write a series of articles about Java 9, about ten articles.

The content of this article: In Java 9, the support for the JAR multi-version bytecode file format is enhanced. The same Jar package can contain multiple Java versions of class files. Using this feature, we can upgrade the application/library to a new Java version without forcing users to upgrade to the same Java version.

1. Basic usage

To release jar packages of multi-version bytecodes, the following statement needs to be made in its MANIFEST.MF:

Multi-Release: true

The META-INF/versionsfile directory of the jar package can contain multiple versions of class files. The directory structure of the compilation result is as follows:

jar root
  - A.class
  - B.class
  - META-INF
     - versions
        - 9
           - A.class

Assume that the root directory above is the bytecode file A.calss compiled with java 8 or earlier versions. META-INF/versions/9/It is the compiled result A.class of the java code written in java 9.

  • If the jar package is run in the runtime environment of JDK 8, the class file under the root directory will be used for program execution.
  • If the jar package is running in the runtime environment of JDK 9, the META-INF/versions/9/following class file will be used for program operation.

Assuming that this project will upgrade JDK 10 in the future and decide to use some of the new features of Java 10 in A.java, you can separately upgrade the syntax for A.class, and place the compiled result a.class META-INF/versions/10/below

jar root
  - A.class
  - B.class
  - META-INF
     - versions
        - 9
           - A.class
        - 10
           - A.class

Now, the above jar contains bytecode files that can be run in three Java versions, and A.class is compatible with JDK 8, 9, 10.

Two, real examples

java 8 code

The following class file code we let it run in the environment of Java 8

package com.example;

public class IOUtil {
  public static String convertToString(InputStream inputStream) throws IOException {
      System.out.println("IOUtil 使用java 8 版本");
      Scanner scanner = new Scanner(inputStream, "UTF-8");
      String str = scanner.useDelimiter("\\A").next();
      scanner.close();
      return str;
  }
}

Add a Main.java application entry file, call IOUtil.convertToString method to convert InputStream to String.

package com.example;

public class Main {
  public static void main(String[] args) throws IOException {
          InputStream inputStream = new ByteArrayInputStream("测试字符串".getBytes());
          String result = IOUtil.convertToString(inputStream);
          System.out.println(result);
      }
}

Java 9 code

After the release of Java 9, we decided to rewrite the IOUtil.convertToString method using Java 9's new syntax.

package com.example;

public class IOUtil {
  public static String convertToString(InputStream inputStream) throws IOException {
      System.out.println("IOUtil 使用java 9 版本");
      try (inputStream) {  //Java9版本的增强try-with-resources
          String str = new String(inputStream.readAllBytes());
          return str;
      }
  }
}

As shown in the code above, we use the two new features of Java 9 try-with-resource block with inputStream reference and the new InputStream.readAllBytes() method.

Compile

Compile the IOUtil.java code of Java8 and Java9 into class bytecode files under the versions of JDK8 and JDK9 respectively, and save the class files according to the following directory structure, and type the jar package. (First type the jar package according to the java8 version, then modify the MANIFEST.MF file and add the java 9 bytecode class file)

D:\multi-release-jar-example\my-lib-jar>tree /A /F
+---com
|   \---example
|           IOUtil.class
|           Main.class
|           
\---META-INF
    |   MANIFEST.MF
    |   
    \---versions
        \---9
            \---com
                \---example
                        IOUtil.class

Run Main class

Run this jar package in the JDK 9 environment

D:\multi-release-jar-example>java -cp my-lib.jar com.example.Main
IOUtil 使用java 9 版本
测试字符串

Run this jar package in the JDK 8 environment

D:\multi-release-jar-example>C:\jdk1.8.0_151\bin\java -cp my-lib.jar com.example.Main
IOUtil 使用java 8 版本
测试字符串

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/109109168