How To Use Source and Target Parameter in Java Compiler
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.
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.
About Krishna Srinivasan
He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.
Setting the -source and -target of the Java Compiler
Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target . The Compiler Plugin can also be configured to provide these options during compilation.
For example, if you want to use the Java 8 language features ( -source 1.8 ) and also want the compiled classes to be compatible with JVM 1.8 ( -target 1.8 ), you can either add the two following properties, which are the default property names for the plugin parameters:
Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler’s boot classpath to match the target JRE, or use the Animal Sniffer Maven Plugin to verify your code doesn’t use unintended APIs, or better yet use the release option supported since JDK 9. In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch Maven, refer to the Compile Using A Different JDK example.