Part 3 of Java 9 series - the same Jar supports multiple JDK versions

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

Content of this article: in Java 9, the support of jar multi version bytecode file format is enhanced. The same jar package can contain multiple java version 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

Multi version bytecode distribution jar package needs to be in its manifest MF makes the following statement:

Multi-Release: true

Multiple versions of class files can be included in the META-INF/versions file directory of jar package. The directory structure of compilation results is as follows:

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

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

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

Suppose this project upgrades JDK 10 in the future and decides to use some new features of Java 10 in A.java. You can upgrade the syntax of a.class separately, and place the compiled result a.class under META-INF/versions/10 /

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

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

2, 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 use java 8 edition");
      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 The converttostring method converts InputStream to String.

package com.example;

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

Java 9 code

After the release of Java 9, we decided to rewrite ioutil. With the new syntax of Java 9 Converttostring method.

package com.example;

public class IOUtil {
  public static String convertToString(InputStream inputStream) throws IOException {
      System.out.println("IOUtil use java 9 edition");
      try (inputStream) {  //Enhanced try with resources for Java 9
          String str = new String(inputStream.readAllBytes());
          return str;
      }
  }
}

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

compile

Put the ioutil of java8 and Java9 The Java code is compiled into class bytecode files under the versions of JDK8 and JDK9 respectively, and the class files are marked and saved according to the following directory structure, and the jar package is marked. (first make a 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 the jar package in the JDK 9 environment

D:\multi-release-jar-example>java -cp my-lib.jar com.example.Main
IOUtil use java 9 edition
 Test string

Run the 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 use java 8 edition
 Test string

Welcome to my blog. There are many collections of boutiques in it

Reprint of this article shall indicate the source (it must be connected, not just text): Letter brother blog - zimug.com

If you think it's helpful to you, please give me some praise and share! Your support is my inexhaustible creative power. In addition, the author recently output the following high-quality content, looking forward to your attention.

Tags: Spring

Posted by Marqis on Wed, 11 May 2022 09:34:42 +0300