Explain Codes LogoExplain Codes Logo

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

java
gradle
intellij
jdk
Alex KataevbyAlex Kataev·Oct 20, 2024
TLDR

No time to read? Don't worry, just update Gradle to use the JDK 8. Set the JAVA_HOME to your JDK 8 executable path, or directly in your gradle.properties:

org.gradle.java.home=C:/Java/jdk8

Replace C:/Java/jdk8 with your specific JDK 8 install location, and Gradle will target the appropriate Java version.

In IntelliJ IDEA version 2018.1.2, you can accomplish this by updating your IDE settings. Navigate to Build Tools -> Gradle -> Gradle JVM and update this to the verified JDK 1.8 installation.

Getting versions in order: Gradle, JDK, and IntelliJ

Successful builds require harmonized versions of Gradle, the JDK, and IntelliJ IDEA. How do you achieve this? Follow the guide below:

Updating Gradle's compatibility version

In your build.gradle, ensure that sourceCompatibility and targetCompatibility are being set to 1.8:

sourceCompatibility = 1.8 // Like having coffee with an old friend targetCompatibility = 1.8 // Ensuring you're speaking the same language

Making the best use of Gradle Wrapper

Using the Gradle Wrapper can provide a more consistent experience across teams. Run gradlew wrapper --gradle-version X.Y to set the desired wrapper version.

gradlew wrapper --gradle-version 7.2 // Latest stable at the time of writing 🚀

IntelliJ IDEA version compatibility

  • In IntelliJ, confirm the correct Gradle JVM is selected under Preferences | Build, Execution, Deployment | Build Tools | Gradle.
  • If using an older IntelliJ version, it's time to hit that update button. Newer Java versions need the latest and greatest IDE version.

Fine-tuning your JDK setup

  • If you're still seeing mismatches or issues even after updating JDK, take a moment to verify your project's targeting Java version.
  • Not afraid of trial and error? Play around with different JDK installations by changing the JDK path in gradle.properties.

Keeping your properties in check

  • In gradle.properties, erase any hardcoded properties that may cause chaos, such as an old definition for org.gradle.java.home.
org.gradle.java.home= // Imagine this line wearing an "I'm with stupid" T-shirt

Bridging the environment gap

  • Aim for a uniform environment setup across different machines. Utilize a Docker container, a VM with the correct JDK, or stick to the Gradle Wrapper.
  • When working on a multi-project setup, nip version drift in the bud by managing JDK and Gradle versions in the root build.gradle.

Keeping IntelliJ IDEA in good shape

  • For extra performance, tweak IntelliJ configurations to use a local Gradle distribution rather than the pre-packaged version.
  • Peculiarity identified - IntelliJ's Build Tools preferences can affect how the IDE interacts with Gradle.

Common pitfalls and their avoidance

  • Make sure no version-specific configurations or scripts in your build.gradle are hijacking the JVM setup.
  • If you're a heavy user of features like incremental compilation, check your toolchain configurations as they might have hidden JDK dependencies.