Gradle - Could not target platform: 'Java SE 8' using tool chain: 'JDK 7 (1.7)'

JavaIntellij IdeaGradle

Java Problem Overview


I am trying to import Gradle project in Intellij Idea with local Gradle distrib and getting stacktrace with the following message: Could not target platform: 'Java SE 8' using tool chain: 'JDK 7 (1.7)'. Could anyone explain please what could be the reason?

Java Solutions


Solution 1 - Java

This is what worked for me (Intellij Idea 2018.1.2):

  1. Navigate to: File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

  2. Gradle JVM: change to version 1.8

  3. Re-run the gradle task

Solution 2 - Java

For IntelliJ 2019:

Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM

Select correct version.

Solution 3 - Java

For IntelliJ 2019, JDK 13 and gRPC:

Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM

and Select correct version.

you might also have to adding below line in your build.gradle dependencies

compileOnly group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'

Solution 4 - Java

Finally I imported my Gradle project. These are the steps:

  1. I switched from local Gradle distrib to Intellij Idea Gradle Wrapper (gradle-2.14).
  2. I pointed system variable JAVA_HOME to JDK 8 (it was 7th previously) as I had figured out by experiments that Gradle Wrapper could process the project with JDK 8 only.
  3. I deleted previously manually created file gradle.properties (with org.gradle.java.homevariable) in windows user .gradle directory as, I guessed, it didn't bring any additional value to Gradle.

Solution 5 - Java

Since I had to compile some source with 7 compatibility, because of some legacy system and ran into the same problem. I found out that in the gradle configuration there where two options set to java 8

sourceCompatibility = 1.8
targetCompatibility = 1.8

switching these to 1.7 solved the problem for me, keeping JAVA_HOME pointing to the installed JDK-7

sourceCompatibility = 1.7
targetCompatibility = 1.7

Solution 6 - Java

you have to change the -> sourceCompatibility = '1.7' in build.Gradle

Solution 7 - Java

I had a very related issue but for higher Java versions:

$ ./gradlew clean assemble

... <other normal Gradle output>

Could not target platform: 'Java SE 11' using tool chain: 'JDK 8 (1.8)'.

I noticed that the task succeeded when running using InteliJ. Adding a file (same level as build.gradle) called .java-version solved my issue:

 # .java-version
 11.0.3

Solution 8 - Java

The following worked for me:

  1. Go to the top right corner of IntelliJ -> click the icon
  2. In the Project Structure window -> Select project -> In the Project SDK, choose the correct version -> Click Apply -> Click Okay

Solution 9 - Java

Adding to all information above to change the JVM version on IntelliJ settings, I'd like to mention that you have to do that for all modules of your project.

So if you are working on a multimodule project, make sure you changed the JVM version for all of them.

Solution 10 - Java

And so I see from other answers that there are several ways of dealing with it. But I don't believe this. It has to be reduced into one way. I love IDE but, but if I follow the IDE steps provided from different answers I know this is not the fundamental algebra. My error looked like:

* What went wrong:
Execution failed for task ':compileJava'.
> Could not target platform: 'Java SE 11' using tool chain: 'JDK 8 (1.8)'.

And the way to solve it scientifically is:

vi build.gradle

To change from:

java {
    sourceCompatibility = JavaVersion.toVersion('11')
    targetCompatibility = JavaVersion.toVersion('11')
}

to become:

java {
    sourceCompatibility = JavaVersion.toVersion('8')
    targetCompatibility = JavaVersion.toVersion('8')
}

The scientific method is that method that is open for argumentation and deals on common denominators.

Solution 11 - Java

Although this question specifically asks about IntelliJ, this was the first result I received on Google, so I believe that many Eclipse users may have the same problem using Buildship.

You can set your Gradle JVM in Eclipse by going to Gradle Tasks (in the default view, down at the bottom near the console), right-clicking on the specific task you are trying to run, clicking "Open Gradle Run Configuration..." and moving to the Java Home tab and picking the correct JVM for your project.

Solution 12 - Java

Java 9 JDK 9.0.4

  1. Go to the top left corner of Android Studio (4.1.1)-> click on File
  2. Click on Setting
  3. Click on Build, Execution, Deployment
  4. Click on Compiler
  5. Click on Kotlin Compiler
  6. Target JVM version

File | Settings | Build, Execution, Deployment | Compiler | Kotlin Compiler

Solution 13 - Java

If you are running gradle build from IntelliJ terminal do followings:

  1. Check what java version JAVA_HOME is pointing to with command: INBENJKUMARLM1:payment-service jkuma00$ echo $JAVA_HOME /Users/jkuma00/.sdkman/candidates/java/11.0.11.9.1-amzn
  2. Check source compatibility version in build.gradle file. sourceCompatibility = JavaVersion.VERSION_11

ROOT CAUSE: Major java version reported by $JAVA_HOME must match with java version value which sourceCompatibility property has. If there is a mismatch. This issue will happen.

RESOLUTION: Either change source compatiblity version or change JAVA_HOME value. Motive is to match these versions and everything should be okay.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVasiliy VlasovView Question on Stackoverflow
Solution 1 - JavaMaheshView Answer on Stackoverflow
Solution 2 - JavaFrançois GaudinView Answer on Stackoverflow
Solution 3 - JavaAheza DesireView Answer on Stackoverflow
Solution 4 - JavaVasiliy VlasovView Answer on Stackoverflow
Solution 5 - JavaXtroceView Answer on Stackoverflow
Solution 6 - JavaYasiru PadmasiriView Answer on Stackoverflow
Solution 7 - JavaAdan AmarillasView Answer on Stackoverflow
Solution 8 - Javamira kabraView Answer on Stackoverflow
Solution 9 - JavaLuis Felipe MartinsView Answer on Stackoverflow
Solution 10 - JavaeigenfieldView Answer on Stackoverflow
Solution 11 - Javasam1370View Answer on Stackoverflow
Solution 12 - JavaRAKESH DADHICHView Answer on Stackoverflow
Solution 13 - JavaJITUView Answer on Stackoverflow