Java 9's javac
has a new flag --release
:
> javac --help
...
--release <release>
Compile for a specific VM version. Supported targets: 6, 7, 8, 9
How is it different from -source
and -target
flags? Is it just a shortcut for -source X -target X
?
--release X
is more than just a shortcut to-source X -target X
because-source
and-target
are not sufficient to safely compile to an older release. You also need to set a-bootclasspath
flag which must correspond to the older release (and this flag is often forgotten). So, in Java 9 they made a single--release
flag which is a replacement for three flags:-source
,-target
and-bootclasspath
.So, this is an example of compiling to Java 1.7:
Note that you don't even need to have JDK 7 installed on your computer. JDK 9 already contains the needed information to prevent you from accidental linking to symbols that did not exist in JDK 7.
Not exactly.
JEP 247: Compile for Older Platform Versions defines this new command-line option,
--release
:So no, it is not equivalent to
-source N -target N
. The reason for this addition is stated in the "Motivation" section:In short, specifying the source and target options are not sufficient for cross-compilation. Because
javac
, by default, compiles against the most recent of the platform APIs, they can't be guaranteed to run on older versions. You also need to specify the-bootclasspath
option corresponding to the older version to cross-compile correctly. This would include the correct API version to compile against and allow for execution on older version. Since it was very often forgotten, it was decided to add one command line option which did all the necessary things to correctly cross-compile.Further reading in the mailing list and Oracle Docs. The original bug was filed here. Note that since the integration of this option, JDK builds have come bundled with descriptions of the platform APIs of older releases, mentioned under section "Risks and Assumptions". That means you don't need the older version installed on your machine for cross-compilation to work.