Gradle: sourceCompatiblity vs targetCompatibility – Gradle: sourceCompatiblity vs targetCompatibility

最后修改: 2022年 2月 16日

1. Overview


In this article, we’ll look at the difference between sourceCompatbility and targetCompatibility Java configurations as well as their usage in Gradle.


You can check our Introduction to Gradle article to learn more about the basics.


2. Handling Versions in Java


When we compile a Java program using javac, we can provide compilation options for version handling. There are two options available:


  • -source with values that match the Java versions, up to the JDK we are using for compilation (for example, 1.8 for JDK8). The version value we provide will restrict the language features that we can use in our source code to its respective Java version.
  • -target is similar but controls the version of the generated class files. This means that the version value we provide will be the lowest Java version our program can run on.

For example:


javac -source 1.6 -target 1.8

This will generate a class file that requires Java 8 or above to run. Additionally, the source code cannot contain lambda expressions or any feature not available in Java 6.

这将产生一个需要Java 8或以上版本才能运行的类文件。此外,源代码不能包含lambda表达式或任何Java 6中没有的功能

3. Handling Versions With Gradle


Gradle, along with the Java plugin, lets us set the source and target options with the sourceCompatibility and targetCompatibility configurations of the java task. Similarly, we use the same values as we do with javac.

Gradle与Java插件一起,让我们用sourceCompatibilitytargetCompatibility任务的java配置来设置source target选项。同样地,我们使用与javac相同的值。

Let’s set up the build.gradle file:


plugins {
    id 'java'

group 'com.baeldung'

java {
    sourceCompatibility = "1.6"
    targetCompatibility = "1.8"

4. HelloWorldApp Example Compilation

4.HelloWorldApp 编译实例

We can create a Hello World! console app and demonstrate the functionality by building it using the above script.

我们可以创建一个Hello World!控制台应用程序,并通过使用上述脚本构建它来演示功能。

Let’s create a very simple class:


public class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");

When we build it using the gradle build command, Gradle will generate a class file with the name HelloWorldApp.class.

当我们使用gradle build命令构建它时,Gradle将生成一个名为HelloWorldApp.class的类文件。

We can use the javap command-line tool that is packaged with Java to check the generated bytecode version of this class file:


javap -verbose HelloWorldApp.class

This prints a lot of information, but in the first few lines, we can see:


public class com.baeldung.helloworld.HelloWorldApp
  minor version: 0
  major version: 52

The major version field has the value 52, which is the version number for Java 8 class files. This means that our HelloWorldApp.class can only run using Java 8 and above.

主要版本字段的值是52,这是Java 8类文件的版本号。这意味着我们的HelloWorldApp.class只能使用Java 8及以上版本运行

To test the sourceCompatibility configuration, we can change our source code and introduce a feature that is not available in Java 6.

为了测试sourceCompatibility配置,我们可以改变我们的源代码并引入Java 6中没有的功能。

Let’s use a lambda expression:


public class HelloWorldApp {

    public static void main(String[] args) {
        Runnable helloLambda = () -> {
            System.out.println("Hello World!");


If we try to build our code with Gradle, we’ll see a compilation error:


error: lambda expressions are not supported in -source 1.6

The -source option, which is the Java equivalent of the sourceCompatibility Gradle configuration, prevents our code from compiling. Basically, it protects us from using higher version features by mistake if we don’t want to introduce them – for example, we may want our app to be able to run on Java 6 runtimes as well.

-source选项,相当于Java的sourceCompatibilityGradle配置,防止我们的代码被编译。基本上,如果我们不想引入更高版本的功能,它可以保护我们不误用这些功能–例如,我们可能希望我们的应用程序也能在Java 6运行时运行。

5. Conclusion


In this article, we explained how to use the -source and -target compilation options to handle the versions of our Java source code and the target runtime. In addition, we learned how these options map to Gradle’s sourceCompatbility and targetCompatibility configurations with the Java plugin and demonstrated their functionality in practice.


As always, the source code for this article is available over on GitHub.