Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error:java: invalid source release IntelliJ IDEA

I use IntelliJ IDEA (2018.1.2). When try to run anything I get this error "Error:java: invalid source release:". I can't compile anything.

The output of "java --version":

java 15.0.1 2020-10-20
Java(TM) SE Runtime Environment (build 15.0.1+9-18)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)

My IDE settings:

Settings -> Build, Exectution, Deployment -> Compiler -> Java Compiler -> Target bytecode version = 11
Project Structure -> Project SDK = 15.0.1
Project Structure -> Project language level = X - experimental features
Project Structure -> Modules -> Language level (root) = X - experimental features

I have tried change any parameter above to a lower value but no result. The IDE says it uses Java 8, and I don't know how to change it.

like image 752
sfaasdfgdsagsdavbs Avatar asked Nov 25 '25 00:11

sfaasdfgdsagsdavbs


1 Answers

tl;dr

You specified different versions of Java. 11 differs from 15.0.1.

You must be consistent with your desired version of Java across several settings.

Spelunking

Unfortunately IntelliJ does not make it easy to specify your desired version of Java. You have to go spelunking in multiple places for multiple settings. You must make them agree.

Use either the current version (now Java 23), or one of the LTS versions: 8, 11, 17, 21, and likely 25.

IDE Settings

As of IntelliJ IDEA 2024.3 RC

In the IntelliJ Settings, set Build, Execution, Deployment > Compiler > Java Compiler > Project Bytecode version to Same as language level. Set Target bytecode version to your number (23 seen here). Note that Target bytecode version pop-up menu offers Same as language level but that seems to be broken.

screenshot of Build, Execution, Deployment > Compiler > Java Compiler > Project Bytecode version

Project Settings

In File > Project Structure > Project Settings > Project, set SDK. Then set Project > Language Level to SDK default.

screenshot of File > Project Structure > Project Settings > Project

In File > Project Structure > Project Settings > Modules, set Module SDK to Project SDK.

screenshot of File > Project Structure > Project Settings > Modules

Maven POM

In your Maven POM, you should find either a pair of source & target elements, or a single release element. Set the version number there to match your other settings.

Tip: If you see source & target, the latest tooling will allow you to replace those with a single release element, if you so desire.

screenshot of POM

Gradle

If you are using Gradle rather than Maven, set the version number similarly in the appropriate build configuration file.

Obtaining a JDK

FYI, IntelliJ can locate and download a JDK from any of multiple vendors on your behalf.

I prefer to use SDKMAN!, a nice little utility for installing JDKs, JREs, and similar software kits such as Maven & Gradle on Unix-oriented OSes such as macOS, Linux, etc.

like image 55
Basil Bourque Avatar answered Nov 26 '25 15:11

Basil Bourque