Gradle: Cannot compile to Java 8 with Java 11 source

Created on 11 Dec 2018  路  3Comments  路  Source: gradle/gradle


I try to compile to Java 8 from Java 11 source by setting the targetCompatibility = 1.8 in the build file.

Here is the content of the build.gradle file

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

Expected Behavior

From the documentation describing the property targetCompatibility below, I expect the build to successfully build classes that can be run on Java 8 environment with targetCompatibility = 1.8 set inside the build.gradle file.

Defines the minimum JVM version your code should run on, i.e. it determines the version of byte code the compiler generates.

Current Behavior



When I ran the following command ./gradlew clean build, I got the following error

> Task :compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> warning: source release 11 requires target release 11

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s
2 actionable tasks: 1 executed, 1 up-to-date

Context

The project I am working requires that I compile with JDK 11. However, it has to be compiled to be able to run on JDK 8 environment for the time being until the move to .

Steps to Reproduce (for bugs)



Please see the attached file demo.zip. This is a Spring Boot project generated by Spring Initializr.

Your Environment

Most helpful comment

sourceCompatibility cannot be higher than targetCompatibility, this is a restriction of javac.

You can compile Java 11 bytecode from Java 8 source code, but not the other way round.

All 3 comments

sourceCompatibility cannot be higher than targetCompatibility, this is a restriction of javac.

You can compile Java 11 bytecode from Java 8 source code, but not the other way round.

"Cross-Compilation Options for javac

By default, for releases prior to JDK 9, classes were compiled against the bootstrap classes of the platform that shipped with thejavac command. But javac also supports cross-compiling, in which classes are compiled against bootstrap classes of a different Java platform implementation. It鈥檚 important to use the -bootclasspath and -extdirs options when cross-compiling."
https://docs.oracle.com/en/java/javase/11/tools/javac.html#GUID-AEEC9F07-CB49-4E96-8BC7-BCC2C7F725C9

@mz0 What you copied is unrelated to the question asked above. @larsgrefer answer was correct.

Was this page helpful?
0 / 5 - 0 ratings