Compiling for a different version of Java
suggest changeThe Java programming language and its runtime have undergone numerous changes since the initial public release. These changes include:
- Changes in the Java programming language syntax and semantics
- Changes in the APIs provided by the Java standard class libraries.
- Changes in the Java (bytecode) instruction set and classfile format.
With very few exceptions (for example the enum keyword, changes to some “internal” classes, etc), these changes are backwards compatible.
- A Java program that was compiled using an older version of the Java toolchain will run on a newer Java platform without recompilation.
- A Java program that was written in an older version of Java will compile successfully with a new Java compiler.
Compiling old Java with a newer compiler
If you need to (re-)compile older Java code on a newer Java platform to run on the newer platform, you generally don’t need to give any special compilation flags. In a few cases (e.g. if you had used enum as an identifier) you could use the -source option to disable the new syntax. For example, given the following class:
public class OldSyntax {
private static int enum; // invalid in Java 5 or later
}
the following is required to compile the class using a Java 5 compiler (or later):
$ javac -source 1.4 OldSyntax.java
Compiling for an older execution platform
If you need to compile Java to run on an older Java platforms, the simplest approach is to install a JDK for the oldest version you need to support, and use that JDK’s compiler in your builds.
You can also compile with a newer Java compiler, but that is more complicated. First, some important preconditions must be satisfied:
- The code you are compiling must not use Java language constructs that were not available in the version of Java that you are targeting.
- The code must not depend on standard Java classes, fields, methods and so on that were not available in the older platforms.
- Third-party libraries that the code depends on must also be built for the older platform and available at compile time and run time.
Given the preconditions are met, you can recompile code for an older platform by setting both -source and -target. For example,
$ javac -source 1.4 -target 1.4 SomeClass.java
produces bytecodes that are compatible with a Java 1.4 or later JVM. -source implies a compatible -target when -target is omitted, but -target alone does not lower the source level. A newer compiler then rejects the command because the default source release is newer than the target.
If you use only -target or -source, you still compile against the standard class libraries provided by the compiler’s JDK. The classes can have the right bytecode version and still depend on APIs that the older platform does not have. Before JDK 9, point -bootclasspath at the older runtime:
$ javac -source 1.4 -target 1.4 -bootclasspath path/to/java1.4/rt.jar SomeClass.java
That compiles against the alternative runtime libraries. A dependency on a newer API becomes a compilation error. On JDK 9 and later, -bootclasspath is gone; javac --release 8 (or another release) sets the source, target, and platform APIs together. Very old values such as 1.4 are rejected by current JDKs.