The Java programming language (and its runtime) has undergone numerous changes since its release since its initial public release. These changes include:
With very few exceptions (for example the enum
keyword, changes to some “internal” classes, etc), these changes are backwards compatible.
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
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 there are complicated. First of all, there some important preconditions that must be satisfied:
Given the preconditions are met, you can recompile code for an older platform using the -target
option. For example,
$ javac -target 1.4 SomeClass.java