e-targeting-multiple-java-versions-with-multi-release-jars

E Targeting multiple Java versions with multi-release JARs

 

This appendix covers:

  • what multi-release JARs (MR-JARs) are
  • creating MR-JARs with bytecode for different Java versions
  • how MR-JARs are structured and how JVMs process them
  • when and how to best use them

It is never easy to decide which Java version to require for your project. On the one hand you want to give users the freedom of choice, so it would be nice to support several major versions, not just the newest one. On the other hand you’re dying to use the newest language features and APIs. From Java 9 on, there’s a new JVM feature, multi-release JARs, that helps you reconcile these opposing forces—at least under some circumstances.

Multi-release JARs allow you to ship bytecode for different Java versions in the same artifact. You can then rely on the JVM to load the classes that you compiled for the most recent version it supports. Starting with a project that runs successfully on your minimally required version, you can selectively improve it on newer JVMs by using more resilient and performant APIs—without being forced to raise your project’s baseline.

E.1  Creating a multi-release JAR

E.2  Internal workings of multi-release JARs

E.3  Usage recommendations

E.3.1  Organizing the source code

E.3.2  Organizing the byte code

E.3.3  When to use multi-release JARs

E.4  Summary