Stack Exchange
Error:java: invalid flag: -release
I use Java :
Java(TM) SE Runtime Environment (build 9-ea+143) Java HotSpot(TM) 64-
Bit Server VM (build 9-ea+143, mixed mode)

and IntelliJ IDEA:
IntelliJ IDEA 2016.2.5 Build #IU-162.2228.15, built on October 14,
2016

Get error when compiling: 
Error:java: invalid flag: -release

Executing javac -help :
--release <release>
        Compile for a specific VM version. Supported targets: 6, 7, 8, 9

So javac should use two hyphens with release, but Intellij Idea use with one.
Can I override javac options that IDEA use or fix it in some way? 
UPD: Even Build, Execution, Deployment > Compiler > Java Compiler Additional Command Line Parameter doesn't help in this case

You can change compiler settings in Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler.

@fRoStBiT i know about this option - but now success with it- still Error:java: invalid flag: -release

Have you tried to use Eclipse Compiler?

Got weird NPE in case of Eclipse Compiler

Have you tried 2016.3 (currently in Public Preview)?

@NicolaiParlog just have downloaded IDEA today and it was 2016.2.5

Java 9 support is cutting edge so maybe it's better in the coming version - why not give it a try?

@NicolaiParlog now it works correct! thanks! I even don't have to specify --module-path in compiler = ) btw IDEA doesn't recognize java.lang classes automatically, but compilation is success. Could you present you tip as an answer to allow me mark it as correct and close question?

Java 9 support is cutting edge and should be tried with the most current version. At the moment, this is the public preview of 2016.3, available here.

I'm running IntelliJ 2017.1.3 and getting the same error.

Try version 2017.3.4 or above.

I'm runningIntelliJ 2023.2.1 and getting the same error.

I was using Java 8 and Intellij 2018.1.1
I solved the issue in this way:

open IntelliJ Idea settings 
navigate to Build, Execution, Deployment > Compiler > Java Compiler
Uncheck the option: Use '--release' option for cross-compilation (Java 9 and later)



I Had to do the opposite, check the option, select project bytecode version 9.

Had exactly the same Error:java: invalid flag: -release in Intellij 2017.1. Updating to 2017.2.5 resolves the issue.

Can be caused by mismatch of java version on build configuration and module settings.

Can you explain that further (if needed with reference)? Right now, your answer is very broad.

He is right; if you have java 11 and javafx8, you can well run into problems.

The error appear when on pom: <java.version>11</..> but JAVA_HOME points to jdk 1.8

As specified in this intellij thread you can workaround it by setting the project SDK language level to x - Experimental Features


I had the same problem with IntelliJ IDEA Version 2017.1.3
Updated IntelliJ to Version: 2017.3.4 and with the latest version it works fine.

Remove --warning-mode=all (File -> Setting -> Build, Execution, Deployment -> Java Compiler
ViewImage:


Remove the release tag in configuration of maven-compiler-plugin
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            ... #here
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>


This is valid only if you're using Gradle(Not maven). Changing the Gradle JVM to Java 11 matching my $JAVA_HOME value worked for me.


This issue also occurs when java version used to build is not compatible with spring boot version, as for example spring boot version 3.1.0 requires java 17.If project is created with spring boot version 3.1.0 and later in pom.xml java version is downgraded to 1.8 or 11 to build with 1.8 or 11 respectively then you will get this error. So to avoid this create your project with lower spring boot version which supports your java version

You can check the JDK version, if its less than JDK8, upgrade and try again. It solved my issue.

--release is a new option in JDK >= 9 versions

Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

For me, the problem cleared once I set my environment variable
JAVA_HOME=%JAVA_HOME_8%
(Note: JAVA_HOME_8 and JAVA_HOME_11 are env variables that I've created myself to point to their respective versions and easily switch between). Maven (i'm using 3.8.6) seems to point the jdk using the JAVA_HOME environment variable. So even if the IDE and the pom is properly configured, depending on how your compiler/build/run plugins are setup, maven may still manage to feed off of the JAVA_HOME variable)
I've also done the following:

Modify the pom with java.version as "8"
Modify the project sdk to 1.8 (via project structure dialog)
uncheck the cross-compliation flag as mentioned in the primary solution.


The -release flag is for JDK 9 and above. Please view the article from Baeldung.com: Set the Java version in .pom file from project/Setting the Java Version in Maven, the answer is based on this article.
Personally I have the following versions active.
Maven
Apache Maven 3.3.9
Java
jdk1.8.0_202, version 1.8.0_202, vendor: Oracle Corporation
IntelliJ
IntelliJ IDEA 2022.3.1 (Ultimate Edition)
In my case, I wanted to compile a library project to Java version 8, so I changed the source and target to 1.8. If you have a higher version of Java active on your computer, you should be able to build lower Java version projects as long as you specifically specify it in the .pom file of the project.
Note: "What's more, we don't need a JDK 7 installed in our machine. Java 9 already contains all the information for linking the new language features with JDK 7" from Set the Java version in .pom file from project/Setting the Java Version in Maven.

Set maven.compiler.source and maven.compiler.target project properties to the Java version you want the classes to compile to: 

Set the maven-compiler-plugin source and target to Java 8 (you can also use the ${maven.compiler.source} and ${maven.compiler.source} as the version). 


This should fix the error, assuming you are using a working Java JDK with proper support.
