If you are a Java programmer, then one of the common question comes into mind is How do I compile for compatibility with older versions of Java?. When you run your Java program with an older version of the Java runtime environment, you may get the following Java exception if you have not compiled with the right version of Java language:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
In one of my previous example, I have explained about how to check the version number of the Java from the class file. In this tutorial I am going to explain the two parameters, target
and source
which are used for cross compilation and runtime requirements of your Java application.
If you want to ensure the application has to support the minimum runtim environment, then you have to fully understand how to use the source and target parameter for javac. Java compiler javac have the option to set the target runtime environment supported for your application by using the command line arguments target
while compiling the source code.
Javac Source and Target Parameters
Source parameter tells the compiler that the source code will be compiled with the specific version. The official documentation says the source
parameter provides source compatibility with specified release. If you are compiling the source code with Java 1.6, but if the code is having some specific features in the Java 7, then compilation fails. In that case you have to set the source
parameter as the Java 1.7.
The following are the accepted parameter value for the release:
- 1.3 – The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
- 1.4 – The compiler accepts code containing assertions, which were introduced in JDK 1.4.
- 1.5 – The compiler accepts code containing generics and other language features introduced in JDK 5.
- 5 – Synonym for 1.5.
- 1.6 – This is the default value if you are compiling with JDK 6. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
- 6 – Synonym for 1.6.
- 1.7 – This is the default value. The compiler accepts code with features introduced in Java SE 7.
- 7 – Synonym for 1.7.
Target parameter tells the compiler that what is the minimum targeted JVM that can run the class files. If you pass the target
parameter as 1.5, then the class files compatible to run 1.5 and above version of JVM. But, it can not run below 1.5 JVM.
The default for -target depends on the value of -source:
- If -source is not specified, the value of -target is 1.6
- If -source is 1.2, the value of -target is 1.4
- If -source is 1.3, the value of -target is 1.4
- For all other values of -source, the value of -target is the value of -source.
Let’s look at the below example command:
% javac -source 1.6 -target 1.5
The above code conveys the message that, the code base will be compiled with the Java 1.6 version, but the targeted minimum environment support is Java 1.5. In other words, converted class files are compatible to with 1.6 JVM.
Ant – Source and Target
How to use the source and target parameter of javac can be used in the apache ant script?. Let’s look at the below code snippet of ant script:
<target name="compile"> <javac source="1.5" target="1.5" srcdir=.../> </target>
Maven – Source and Target
This section provides the sample code snippet for using the source and target parameters of javac in the maven’s pom.xml file.
<project> [...] <build> [...] <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> [...] </build> [...] </project>
Removing Support for the older versions
There is a JEP 182 which talks about retiring the old version from the support to reduce the cost to maintain multiple versions.
Oracle wants reduce the maintenance costs of javac
, so the new JEP 182 defines a policy for retiring old -source
and -target
options. From JDK 8, use of a source or target of 1.5
or earlier is deprecated and in JDK 9, support for a source or target of 1.5
or earlier will be completely removed.
From JDK 9 and going forward, javac
will use a “one + three back” policy of supported source and target options. Under this policy, javac
will still be able to recognize and process class files of all previous JDKs, going back to version 45.3 class files generated by JDK 1.0.2, which first shipped in 1996.