160

I've created a new Java project in IntelliJ with Gradle that uses Java 17. When running my app it has the error Cause: error: invalid source release: 17.

My Settings

I've installed openjdk-17 through IntelliJ and set it as my Project SDK.

The Project language level has been set to 17 - Sealed types, always-strict floating-point semantics.

enter image description here

In Modules -> Sources I've set the Language level to Project default (17 - Sealed types, always strict floating-point semantics).

modules->sources

In Modules -> Dependencies I've set the Module SDK to Project SDK openjdk-17.

modules->dependencies

In Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler I've set the Project bytecode version to 17.

enter image description here

Gradle

plugins {
    id 'org.springframework.boot' version '2.5.6'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-websocket'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
}

test {
    useJUnitPlatform()
}

I've looked at all of the answers here but I can't seem to fix this. I must be missing something but I can't find it. I've not had any problems using Java 8 or 11.

How do I resolve this?

Michael
  • 3,411
  • 4
  • 25
  • 56

15 Answers15

360

In intellij just set Gradle JVM to Java version 17.

"File -> Settings.. -> Build, Execution, Deployment -> Build Tools -> Gradle" there select your project and set Gradle JVM to your java 17.0.1

Rakesh R
  • 3,616
  • 1
  • 7
  • 4
  • 19
    Menu "IntelliJ Idea -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle" in macOS – leojh Jan 20 '22 at 00:44
  • 3
    There is the menu, but no selectable field for project and Gradle JVM in the menu :( (Windows10 x64, Intellij 2021.3.1 Ultimate) – Jay Lim Jan 27 '22 at 08:11
  • 2
    How can I bookmark this? I always need to come back to this. – Jorge Tovar Jul 06 '22 at 21:46
  • 2
    You might also want to change the project SDK from File -> Project Structure (⌘;) -> Project Settings -> Project -> SDK. Otherwise, you might get `java.lang.UnsupportedClassVersionError` – Saiteja Parsi Sep 08 '22 at 23:20
51

A picture is worth a thousand words!

Go to preferences and change Gradle JVM.

enter image description here

Jorge Tovar
  • 1,374
  • 12
  • 17
39

Set Gradle JVM to target build jvm

File -> Settings.. ->enter image description here

abhinavsinghvirsen
  • 1,853
  • 16
  • 25
29

The message typically entails that your JAVA_HOME environment variable points to a different Java version.

Here are the steps to follow:

  • Close IntelliJ IDEA
  • Open a terminal window and check your JAVA_HOME variable value:
    • *nix system: echo $JAVA_HOME
    • Windows system: echo %JAVA_HOME%
  • The JAVA_HOME path should be pointing to a different path, then set it to the openjdk-17 path:
    • *nix system: export JAVA_HOME=/path/to/openjdk-17
    • Windows system: set JAVA_HOME=path\to\openjdk-17
  • Open your project again in IntelliJ IDEA
  • Make sure to set both source and target compatibility versions (not only the sourceCompatibility)

You should be able to build your project.

EDIT: Gradle Toolchain

You may need also to instruct Gradle to use a different JVM than the one it uses itself by setting the Java plugin toolchain to your target version:

// build.gradle
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • I've set the Java home to the openjdk installed by IntelliJ. `echo %JAVA_HOME%` `C:\Users\Michael\.jdks\openjdk-17.0.1`. I've then restarted IntelliJ, Added the targetCompatibility as `17` and rerun gradle. When executing my app I still get the error. – Michael Oct 24 '21 at 12:23
  • As another thing to note. If I set it to use Java 11 and leave my `JAVA_HOME` pointing at 17, it works fine. There seems to be something different that it's not liking. – Michael Oct 24 '21 at 12:39
  • How are you launching the application? Did you run the `echo %JAVA_HOME%` command from within the *IntelliJ IDEA* inclosed terminal or a separate terminal application (cmd / powershell...)? – tmarwen Oct 24 '21 at 13:46
  • I'm running a main method from within IntelliJ. If I run `echo %JAVA_HOME%` it outputs `%JAVA_HOME%` in the IntelliJ terminal. In cmd it outputs the correct Java 17 directory. If I set the source compatability to 11 and use the Java 17 JDK it works correctly so it is finding the JDK fine. – Michael Oct 24 '21 at 14:13
  • 2
    You may need to add the `toolchain` configuration to avoid having Gradle using its default one (this may be why it succeeds when setting the sourceCompatibility to 11) – tmarwen Oct 24 '21 at 14:24
  • That is exactly it. Thank you! – Michael Oct 24 '21 at 14:28
  • You save my 2 hours. I'm using windows and the command `set JAVA_HOME=~` not working for me. So I changed system variable directly using windows control panel and everything works perfect. – Jay Lim Jul 31 '22 at 06:09
  • 2
    For IntelliJ Users: Go to Settings -> Build, Execution, Deployment -> Build Tools -> Gradle and set the Gradle JVM to your desired java version. – dnl.re Aug 18 '22 at 08:02
  • Thanks for adding `toolchain` to the answer as I had no control on Gradle's JVM in the Jenkins build pipeline. – CᴴᴀZ Aug 18 '22 at 08:38
4

There could be many reasons due to which this error is thrown by intellij

  • Check your JAVA_HOME ->
echo $JAVA_HOME

if JAVA_HOME is not correct, update the JAVA_HOME environment variable.

  • check if the project structure in IntelliJ is correct.
File > Project Structure > Project Setting > Project -> SDK and Language level should match

File > Project Structure > Project Setting > Modules -> Language Level should match with SDK
  • Check and fix settings in preferences are correct
Preferences > Build, Execution and Deployment > Gradle > Gradle JVM should match with SDK (Preferences > Build, Execution and Deployment > maven if you're using maven)

Preferences > Build, Execution and Deployment > Compiler > Java Compiler > Project Bytecode version (this should match with your SDK as well)
Sumit Jadiya
  • 637
  • 6
  • 7
3

in build.gradle set sourceCompatibility = '11' not sourceCompatibility = '17'

  • 2
    Welcome to SO! Please take a look at the other answers that were given before. Your approach is mentioned there already. In order to keep the site clear and make it easy to find answers, we try to avoid double answers. – ahuemmer Aug 29 '22 at 07:56
2

Set JAVA_ HOME TO JDK 17 and check this by Opening cmd -> javac. This should return the current version of java set in your machine

  • I've done this and restarted IntelliJ. I still get the same error. If I echo the `JAVA_HOME` it looks correct. `echo %JAVA_HOME% C:\Users\Michael\.jdks\openjdk-17.0.1` – Michael Oct 24 '21 at 12:26
  • https://docs.gradle.org/current/userguide/compatibility.html see this compatibility matrix make sure that you are using this . Try running it once on jdk 16 . – Virbhadra Kaulwar Oct 25 '21 at 18:59
  • For MacOS, an example you could stick in your `~/.profile` file: `export JAVA_HOME=$(/usr/libexec/java_home)` – MarkHu Nov 29 '22 at 22:30
2

Below process worked for me: File -> Project Structure -> Platform Settings -> SDKs -> jdk-17.0.5[for me](ADD JDK Required version either from disk or download it)

Boney
  • 1,462
  • 1
  • 11
  • 19
1

If you start your project from a main method directly invoked from Intelij then the missing dialog execution configurations may be the cause of the error.

Always check the execution configurations to make sure the correct JRE folder is plugged in. In latest versions of jdk there is not a specific jre folder but the complete jdk package, so make sure it points at this folder.

enter image description here

enter image description here

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
1

you can define org.gradle.java.home in ~/.gradle/gradle.properties like:

org.gradle.java.home=/path/to/jdk17

this will cover both gradle cli and default value for IntelliJ (if not specified otherwise)

(windows users file at C:\Users\<username>\.gradle\gradle.properties)

ezer
  • 984
  • 6
  • 10
1

I was running into this issue, only when building from terminal, while the execution from the IntelliJ Run Configuration works

The reason is probably, that I had downloaded the JDK from the relatively new way of doing it directly from the IDE

enter image description here

My guess is, that this JDK with its path that is set here

enter image description here

Is only known to IntelliJ and likely dynamically passed to Gradle when running a run configuration, and not known to Gradle itself.

I tried to figure out what is happening, so I've added this task which in both cases prints the same path to an older local JDK.

tasks.build.doFirst {
    println "used jdk path: " + System.getenv('JAVA_HOME')
}

C:\Program Files\GraalVM\graalvm-ce-java11-21.1.0

Afterwards I've deleted the old JDK under that path and retried both, run configuration and terminal execution. Now the run configuration still works, while printing the same invalid JDK path, and the terminal execution fails with

ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\GraalVM\graalvm-ce-java11-21.1.0

Please set the JAVA_HOME variable in your environment to match the location of your Java installation.

I had expected the error in both cases, but it seems the JAVA_HOME directory is not necessarily used and in some way other than with the env variable, IntelliJ has to tell Gradle to use the right JDK from the .jdks directory.

This is just an assumption, if anyone has an explanation I'd appreciate it, especially how Gradle is able to know what JDK to use.

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
  • Check under Settings / Build, Execution... / Build Tools / Gradle. There should be a setting Gradle JVM: that needs to be consistent. For me this was used when I marked a task as "After Sync" in the gradle view. Even though the project is set to "Build and run using: IntelliJ" for the after sync task, it runs external Gradle. – Scott Carlson Mar 18 '22 at 16:39
  • @ScottCarlson thanks but the settings in the IDE are generelly only affecting the run configurations of the IDE. If you enter some commands into a terminal (even that of the IDE) it will pick up the JDK that is set in the system environment variables of Windows, and not the JDK you've set in IntelliJ for your project. – Marian Klühspies Aug 19 '22 at 11:56
0

Had the same problem, tried everything said above but no luck.

Then saw this path C:\Program Files\Common Files\Oracle\Java\javapath present in Systesm variable path. Deleting it solved the issue.

Note: Don't forget to restart the terminal.

Dixit Singla
  • 2,540
  • 3
  • 24
  • 39
0

If your project is using maven, go to Run > Edit Configurations.. > Maven & choose your maven project > checked 'Inherit from settings'

this solving my problem.

enter image description here

KLMN
  • 529
  • 4
  • 13
0

Verify that you are using the same JDK version to compile and run the program.

I also faced a similar problem. but changing the gradle JVM did not solve my problem. My problem was that two versions of JDK were installed in my system. the java command run with JDK 17 but the javac command run with JDK 11.

I deleted the JDK 11 and the problem was solved.

0

Set to the (gradle) project config and commit. By saving this, you will help all your project contributors, in contrast to your local IDE Settings.

If you use build.gradle.kts

java {
  toolchain {
    languageVersion.set(JavaLanguageVersion.of(17))
  }
}

If you use build.gradle

java {
  toolchain {
    languageVersion = JavaLanguageVersion.of(17)
  }
}

as few other answers mentioned.

IntelliJ IDEA(⌘,) -> Gradle JVM (in search bar)

The global UI Settigs, as the accepted answer suggests, would also help. But be aware of the instability:

  1. It would still fail if you re-build with a terminal command ./gradlew assemble.
  2. The setting is global. So you would need to be back to it each time you switch between 11 and 17 projects.
  3. You would need to instruct each developer in the team to do the same UI exercise.
epox
  • 9,236
  • 1
  • 55
  • 38