JDK invalid target release

Suppose we just run into this error:

invalid target release: 21

Module example SDK 21 is not compatible with the source version 21.

Upgrade Module SDK in project settings to 21 or higher. Open project settings.

And you use IntelliJ IDEA. The thing is that you need to double check at least four places that you have chosen the proper JDK version.

Step #1

First things first, make sure that you have JDK 21 selected under File -> Project Settings -> Project -> SDK:

Step #2

Then go to Modules section in the left sidebar -> Dependencies tab -> Module SDK. Check you have chosen proper version of SDK for each project module:

Step #3

Next place to ensure that you have chosen the proper version:

  • For Maven. Global Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Runner -> JRE:
  • For Gradle. Global Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Choose project -> Gradle JDK:

Step #4

Finally, if you use Run Configurations, pay attention to Java Options expand section in the bottom of the edit dialog for Run Configuration. You may have JRE field overridden with an incorrect value:

P.S.: Double check some other places in code, where you may have obsolete values like these:

<maven.compiler.source>
<maven.compiler.target>
<java.version>
sourceCompatibility
targetCompatibility
useJavaVersions
Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝

Leave a Reply

Your email address will not be published. Required fields are marked *