Compiling for older platform versions

The Java compiler, javac, was updated in Java 9 to ensure that it can be used to compile Java programs to run on user-selected older versions of the Java platform. As you can see in the following screenshot, javac has several options, including -source and -target. The javac presented in the following screenshot is from Java 8:

javac options in Java 8

The -source option is used to dictate the Java version that's accepted by the compiler. The -target option informs you of which version of class files javac will produce. By default, javac generates class files in the most recent Java version and that of the platform APIs. This can cause a problem when the compiled application uses APIs that are only available in the most recent platform version. This would render the application ineligible to run on older platform versions, despite what is dictated with -source and -target options.

To address the aforementioned problem, a new command-line option is available in Java.  This option is the --release option and, when used, will automatically configure javac to generate class files that link against a specific platform version. The following screenshot shows the javac options with the current Java platform. As you can see, the new --release option is included:

javac options in Java 18.9

Here is the syntax for the new option:

javac --release <release> <source files>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset