34

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

Prags
  • 2,457
  • 2
  • 21
  • 38
Sergii Getman
  • 3,845
  • 5
  • 34
  • 50

14 Answers14

30

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)

Intellij Settings

Roberto
  • 4,524
  • 1
  • 38
  • 30
9

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.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
8

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

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Dunx
  • 81
  • 1
  • 1
5

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

Bohdan
  • 16,531
  • 16
  • 74
  • 68
4

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

Adelin
  • 7,809
  • 5
  • 37
  • 65
2

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.

Etibar - a tea bar
  • 1,912
  • 3
  • 17
  • 30
2

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

ViewImage:
ViewImage

Dharman
  • 30,962
  • 25
  • 85
  • 135
2

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>
William Price
  • 4,033
  • 1
  • 35
  • 54
1

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

u-ways
  • 6,136
  • 5
  • 31
  • 47
Narendra
  • 21
  • 1
1

--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](/help/how-to-answer). – Community Sep 01 '22 at 21:09
1

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.
blackstrype
  • 451
  • 9
  • 14
1

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.

Gradle preferences

Hemanth Gowda
  • 604
  • 4
  • 16
1

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.

  1. Set maven.compiler.source and maven.compiler.target project properties to the Java version you want the classes to compile to: enter image description here

  2. 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). enter image description here

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

fons-000
  • 31
  • 4
1

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