
compile and test the version-independent source tree on the oldest supported Java version.How do you verify whether you got it right?Īs I said early, a formal description is complex, so here's that rule of thumb I promised: To determine whether your particular layout works, mentally (or actually).
#COMPILING JAVA INTO JAR CODE#
Version-dependent source trees then selectively enhance that code for newer versions. These are not technical requirements nothing stops you from targeting Java 9 and putting half of the code into src /main /java and the other half, or even all of it, into src /main /java - 9, but that only causes confusion.īy sticking to the guidelines, you keep the source tree's layout as simple as possible.Īny human or tool looking into it sees a fully functioning project that targets the required JVM version.

(One addendum to the last point: If you're offering a feature that only works on a newer Java version and can't be steered around, having a class that throws errors stating "Operation not supported before Java X" in the "regular" source tree counts as complete - my recommendation is to not simply leave it out because that would make the project tough to compile.) Sticking to these guidelines, you keep the source tree as simple as possible
#COMPILING JAVA INTO JAR HOW TO#
Now that you know how to create multi-release JARs and how they work, I want to give you some recommendations for how to make the most out of them. These JVMs thus shadow version-unspecific class files with the newest version-specific ones they support. They do that "searching backwards" from their own version, meaning a Java 10 JVM looks for code in META -INF /versions / 10, then META -INF /versions / 9, then the root directory. Newer JVMs, however, first look into META -INF /versions and only if they don't find a class there, into the JAR's root. It is not possible to distinguish versions before 9 JVMs of version 8 and earlier don't know anything about META -INF /versions and simply load the classes from the package structure in the JAR's root.Ĭonsequentially, it is not possible to distinguish between different versions before 9. To create an MR-JAR, use the new jar option -release $.

JVMs will then load the code that was included for their version. Multi-release JARs allow you to create a single JAR that contains bytecode for several Java versions. On the other hand you're dying to use the newest language features and APIs.įrom Java 9 on, multi-releas JARs give you an opportunity to reconcile these opposing forces - at least under some circumstances. It's 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.
