193

I am trying to build an application which was built using java 8, now it's upgraded to java 11. I installed Java 11 using an oracle article in my windows machine and I use IntelliJ IDEA 2017 as my IDE.

I changed my system environment variables and set the

JAVA_HOME to C:\Program Files\Java\jdk-11.0.1

And added that to the Path variable.

C:\>java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode) 

When I build my application in IntelliJ, this is what I get:

Information:java: Errors occurred while compiling module 'test-domain_main'
Information: javac 1.8.0_171 was used to compile java sources
Information:1/10/2019 4:21 PM - Compilation completed with 1 error and 0 warnings in 1s 199ms
Error:java: invalid target release: 11

This is what I've tried so far:

  1. I changed .idea/compiler.xml target values from 8 to 11 but that didn't help. Also, verified the Target bytecode version in settings > Build, Execution, Deployment > Compiler > Java Compiler and all my modules are set to 11.

  2. Went to file > Project Structure > SDKs *(currently I have 1.7 and 1.7 listed)* > Add new SDK > JDK > After that, I selected C:\Program Files\Java\jdk-11.0.1 But it errors out with "The selected directory is not a valid home for JDK"

I am not sure if I installed the wrong JDK 11, because in my C:\Program Files\Java\, I see separate JDK and JRE folders for 1.7 and 1.8 but only JDK folder for 11.0.1

Or is it something else I need to change?

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
AMagic
  • 2,690
  • 3
  • 21
  • 33
  • 1
    I guess the 2017 version of intellij doesn't support jdk11 (or maybe it needs a plugin to work), you could try updating to 2018. – Jorn Vernee Jan 10 '19 at 21:48
  • Thanks @JornVernee based on your advice, I updated and resolved the issue. Java 11 works fine with IntelliJ IDEA 2018.3.3 version. – AMagic Jan 10 '19 at 22:34

36 Answers36

334

I've got the same issue as stated by Gryu. Same Intellij 2018 3.3

I was able to start my project by setting (like stated by Grigoriy)

File->Project Structure->Modules ->> Language level to 8 ( my maven project was set to 1.8 java)

AND

File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> 8 also there

I hope it would be useful

Paolo
  • 3,624
  • 2
  • 12
  • 13
  • 2
    This an other similar/possibly-necessary solutions are described in great further detail (with screenshots) at https://stackoverflow.com/a/26009627/1357094 – cellepo Sep 18 '19 at 15:47
  • 21
    For anyone that can't find. `File -> Settings` I found it in `IntelliJ IDEA -> Preferences... -> Build, Execution, Deployment -> Compiler -> Java Compiler` – Jonny Brooks Nov 11 '20 at 10:04
  • But why? Is that a bug of IDEA? – UnixAgain Dec 25 '20 at 04:38
  • I clean installed intellij from official web site with latest version, and generated a clean project with spring initializer and this happening. This is not supposed to happen on a clean install, isn't it? I mean something is wrong. Btw this solution worked perfectly. – Serdar Samancıoğlu Mar 10 '21 at 16:21
  • Under File->Project Structure->Modules, make sure to check both "Sources -> Language level" and "Dependencies -> Module SDK" – xli Aug 11 '21 at 16:23
  • 1
    Gradle config can also cause this. https://stackoverflow.com/a/68579256/1549046 – AtomicBoolean Sep 28 '22 at 14:41
70
  • I've got the same info messages and error message today, but I have recently updated Idea -> 2018.3.3, built on January 9, 2019.
  • To resolve this issue I've changed File->Project Structure->Modules ->> Language level to 10.

  • And check File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler ->> Project bytecode and Per-module bytecode versions. I have 11 there.

  • Now I don't get these notifications and the error.

    It could be useful for someone like me, having the most recent Idea and getting the same error.

Gryu
  • 2,102
  • 2
  • 16
  • 29
  • 3
    This worked for me even though the IntelliJ date is older than the Java release date. File => Project Structure => Project => Project SDK. Added the newly downloaded JDK under `New` and hit `Apply` => `OK` The project started compiling immediately after. – VKB Apr 18 '19 at 23:55
  • This an other similar/possibly-necessary solutions are described in great further detail (with screenshots) at https://stackoverflow.com/a/26009627/1357094 – cellepo Sep 18 '19 at 15:47
54

1 of the 2 methods will fix the problem:

Method 1 (recommended)

Specify the Java version in your build file.

E.g., For Gradle, go to build.gradle file and specify the source and target compatability like mentioned in the docs and click on the refresh button in the Gradle tool window.

For example, to set source and target compatability to Java version 17:

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

Method 2

If building a project through a build system (Maven, Gradle etc..) via command-line works but IntelliJ show Invalid target release error, then do the following,

  1. Close IntelliJ

  2. Go to the directory of the project

  3. Delete the .idea/ directory

  4. Start IntelliJ with the project's directory

This will re-create the .idea/ directory and will no longer show the error.

NOTE: Any repository specific IntelliJ settings that you have added would be deleted when the .idea/ directory is deleted and they will be re-created with the defaults.

neonidian
  • 1,221
  • 13
  • 20
  • 2
    Isn't deleting the `./idea` folder a bit too harsh? All your settings will be gone. – Younes El Ouarti Jul 15 '20 at 16:32
  • 1
    Yes, I agree its a bit harsh. All the repository specific settings would be gone but the global settings like a custom keymap would be preserved since they are stored in the user's home directory. Updated the answer with a note. – neonidian Jul 19 '20 at 08:47
  • Method 2 fixed it for me. thx. – Mulgard Jan 10 '23 at 11:53
44

Below changes worked for me and hope the same works for you.

  1. Change the version of Java from 11 to 8 in pom.xml file
    <java.version>1.8</java.version>

  2. Go to, File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler
    Here, in Module column you can see your project listed and in Target bytecode version column, Java version for the project is already specified(mostly 11), change it to 8

  3. Go to, File -> Project Structure -> Modules. Here, in Source tab, you can see Language level option, choose 8 - Lambdas, type annotations etc.. Finally, choose Apply and OK, you're good to go.

B Sangappa
  • 574
  • 4
  • 8
  • 2
    Only #1 worked for me.. tried many solutions but this worked – Rajni Gangwar Mar 09 '21 at 12:22
  • This works for me. I.e., changed the version in the pom and then also changed both of those places in IntelliJ. Not sure why there are 2 spots within IntelliJ where you need to designate the Java version. – Woodchuck Apr 23 '21 at 18:28
  • Where's pom.xml path? – MML Aug 10 '21 at 02:04
  • Hey @MML, pom.xml is available in the root level of the project itself, for example {Your Project}\pom.xml. Expand your project you will see the pom.xml – B Sangappa Aug 10 '21 at 14:53
  • @b-sangappa I switched to Project view in android studio but I cannot find this file, can I created it manually ? – MML Aug 10 '21 at 15:03
  • @MML, once verify whether file is present or not in Project location of File Explorer itself, if not present, you can create a POM file in root level like {Your Project}\pom.xml – B Sangappa Aug 11 '21 at 08:25
23

Please update to IntelliJ IDEA 2018.x to get Java 11 support. Your IntelliJ IDEA version was released before Java 11 and doesn't support this Java version.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 45
    Even the 2020 version has the same problem. So this is not a correct answer or partially correct. You have to change the sdk version from project structure. – Zafar Aug 26 '20 at 19:22
  • 3
    @Zafar this was the correct answer for the original question and the OP has accepted it. But there can be multiple causes for this error which cannot be covered in a single answer. – CrazyCoder Aug 26 '20 at 19:24
  • That's not the right answer :) or at least not the complete one. You can still run into this issue with newer IntelliJ version as well – Beckster Apr 28 '21 at 15:27
  • Not a solution why whould everything needs to be updated. The 4th solution works most of the time. – Plain_Dude_Sleeping_Alone Oct 03 '22 at 09:42
18

January 6th, 2021

This is what worked for me.

Go to File -> Project Structure and select the "Dependencies" tab on the right panel of the window. Then change the "Module SDK" using the drop-down like this. Then apply changes.

image 1

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
17

There is also the possibility of Maven using a different version of JDK, in that case you can set Maven to use the project default JDK version.

enter image description here

amertkara
  • 1,369
  • 14
  • 19
17

In newer versions of Intellij, check that jdk 11 is correctly configured:

  1. Right click on your project then Open Module Settings. Check the settings listed below:
    • SDKs (Under Platform Settings) > Add the jdk 11 you want located on your computer.
    • Modules (Under Project Settings) > Set language level to jdk 11 for sources and dependencies.
    • Project (Under Project Settings) > Select jdk 11 as Project SDK. enter image description here enter image description here enter image description here
  2. Then File > Settings > Java Compiler. Check the following settings:
    • Use compiler: Javac
    • Project bytecode version: 11
    • Per-module bytecode version: the target bytecode version for all modules must be set to 11. enter image description here
Dharman
  • 30,962
  • 25
  • 85
  • 135
Pieber
  • 393
  • 2
  • 7
  • 13
12

My solution:

  1. Go to File > Settings > Build, Execution, Deployment > Build Tools > Gradle
  2. Select Gradle JVM: [SDK VERSION]
  3. Compile

enter image description here

RiveN
  • 2,595
  • 11
  • 13
  • 26
Enrique Alcocer
  • 121
  • 1
  • 2
9

Got this same error after updating my Idea and I resolved it as follows:

File >> Project Structure... >> Project

“Project language level:” was set to “11 - Local variable syntax for lambda parameters” so I changed it to “SDK default (8 - Lambdas, type annotations etc.)” then applied the change and the error was resolved.

AMS Abu
  • 89
  • 1
  • 1
8

I was recently facing the same problem. This Error was showing on my screen after running my project main file. Error:java: invalid source release: 11 Follow the steps to resolve this error

  1. File->Project Structure -> Project
  2. Click New button under Project SDK: Add the latest SDK and Click OK.

enter image description here

After running You will see error is resolved..

enter image description here

enter image description here

You can see it's work perfectly.. Please approach me If you find any problem

Uzair
  • 317
  • 4
  • 7
5

If you are in Springboot please check the java version in the pom.xml file

<properties>
   <java.version>11</java.version>
</properties>

If this version doesn't match with your default version(Java) of the computer, that error can be happen.

Mafei
  • 3,063
  • 2
  • 15
  • 37
4

I had the issue because I'm currently upgrading my code base to JDK 11 and am switching between 8 and 11 in the branches. It seems IntelliJ doesn't like this.

Solution:

If you've selected the currect JDK in your project structure (Ctrl+Shift+Alt+S) and still get the error, invalidate your cache File > Invalidate Caches / Restart....

After restarting the IDE the error went away in my case.

Younes El Ouarti
  • 2,200
  • 2
  • 20
  • 31
3

For me, I was having the same issue but it was with java v8, I am using a different version of java on my machine for my different projects. While importing one of my project I got the same problem. To check the configuration I checked all my SDK related settings whether it is in File->Project->Project Structure / Modules or in the Run/Debug configuration setting. Everything I set to java-8 but still I was getting the same issue. While checking all of the configuration files I found that compiler.xml in .idea is having an entry for the bytecodeTargetLevel which was set to 11. Here if I change it to 8 even though it shows the same compiler output and removing <bytecodeTargetLevel target="11" /> from compiler.xml resolve the issue.

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Shri
  • 31
  • 2
3

Try this

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
3

If your project is built using gradle, you should update:

File > Settings > Build, Execution, Deployment > Build Tools > Gradle

And update Gradle JVM with version 11.

JFL
  • 1,502
  • 14
  • 18
3

For me, the problem causing this error message was that my JAVA_HOME environment variable was still pointing to the JDK 1.8 directory. I made my JAVA_HOME environment variable pointing to the JDK 11 directory and it worked.

RotS
  • 2,142
  • 2
  • 24
  • 30
2

I Could say I had similar issue. My case: I switched from new version to old version of the project in workspace, try to run single junit which require recompil, recompilation error was thrown with invalid target.

From Projects settings (F4) in IntelliJ everything was looking good and java was set to 1.7. But when I try recompile from IDE error was thrown because of wrong target level. After checking I found that in one of the iml file language was set to JDK_11. After manual change to JDK_1_7 everything back to normal.

Worth to also manual check lang level in *iml files created by IDE.

tarkos
  • 197
  • 1
  • 4
  • I can add that good to check also option like it was in above comments : `File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler ` and review if target code is set correctly. – tarkos Oct 10 '19 at 13:29
  • This keeps on rollbacking to the previous settings. Is there a permanent fix for this? Can this configuration be glued to the project? (If there are multiple projects with multiple versions of Java, How to handle it in code) – Praveen Jan 15 '20 at 09:41
2

My project module was set to 8 but the pom.xml was set to 11. When I changed from 8 to 11 in the module, it worked.

markthegrea
  • 3,731
  • 7
  • 55
  • 78
2

i also got same error , i just change the java version in pom.xml from 11 to 1.8 and it's work fine.

AmitPal
  • 31
  • 2
2

In your pom.xml file inside that <java.version> write "8" instead write "11" ,and RECOMPILE your pom.xml file And tadaaaaaa it works !

2

I tried all the above and found this secret sauce

  1. make sure pom.xml specifies your desired jdk.
  2. make sure maven specifies your desired jdk.
  3. make sure Projects specifies your desired jdk.
  4. make sure Modules specifies your integer jdk AND Dependencies specifies your jdk. hth.
cp.
  • 1,241
  • 5
  • 15
  • 26
2

2021 Jan.12th

the whole point for me is to explicitly specify the version as java 11 in my pom.xml, and I solved the problem by taking the following steps.

  1. open terminal and input: java -version, then i got 1.8, and i went to .bash_profile to switch the java version to 11.2 as i have multiple java version installed. please remember "restart" your terminal and check your java version again to confirm switch is successful.

  2. Then i went to File -> Project Structure to make sure my IntelliJ using the same version as my env, which is 11.2.

  3. RESTART your Intellij, mvn clean install, solved, hope this could help someone that has same issue, thanks.

ItwasJJsmile
  • 111
  • 1
  • 5
2
1. pom.xml (start with this important)
    - <java.version>1.8</java.version>
    * Enable auto import on pop up
    * Wait until it loads

2. Project Structure
    - Project
        - Project SDK - 1.8 
        - Project language Level - 8
    - Modules
        - Sources
            - Language level - 8
            - Dependencies - Project SDK 1.8

3. Delete idea folder
4. Close window
5. Reopen window (make sure it loads at bottom)
joeabala
  • 266
  • 3
  • 11
2

Another solution worked for me, but the root cause was I messed up in maven configuration in the past.

After all suggestions my JAVA_HOME when I called it through terminal shown that I was using Java 11, but when I checked mvn -v I had got information that Maven uses Java 1.8...
That was weird. I went through all suggested IntelliJ settings (Project Structure, Compiler version etc.) and everything was pointing to use Java 11 But finally I found I messed up that cofiguration in the past.
I'm using Mac and in my case the root cause was Overriding default system JAVA_HOME though Maven settings in ~/.mavenrc configuration file. Inside that I had overriden JAVA_HOME property pointing to 1.8 library.
The reason why I did it was a previous problems with maven on other project and I needed to force maven to use Java 1.8 so when I switched to another project it caused finally problems.
I know it's very specific but maybe there is someone else who went through all suggestions and nothing worked, and overrided this variable too.

EnGoPy
  • 333
  • 4
  • 14
1

Just had same error. Problem was that I had empty package-info.java file. As soon as I added package name inside it worked...

Bojan Vukasovic
  • 2,054
  • 22
  • 43
1

I also got a similar error but in my case it was Error:java: invalid source release: 11... So I just changed all the annotations of java 11 to java 8 and even if the annotation was java 8, I just leaved it like that. I hope it helps you to fix the issue.

machete333
  • 11
  • 1
1

I changed file -> project structure -> project settings -> modules In the source tab, I set the Language Level from : 14, or 11, to: "Project Default". This fixed my issue.

seekle
  • 228
  • 2
  • 7
1

I added these two lines to build.gradle file

compileJava.options.fork = true
                                              
compileJava.options.forkOptions.executable = 'C:\\Program Files\\Java\\jdk-11.0.8'

and it works

I am using windows and my project based on gradle

my jdk path -> 'C:\Program Files\Java\jdk-11.0.8'

please provide your jdk path

asifaftab87
  • 1,315
  • 24
  • 28
1

I tried all the solutions above

  • gradle jvm version , java compilter bytecode version, module language.

I set all the above to java 8 (or 1.8) as that is the library I have on my machine. It still did not work.

The issue was that the build.gradle file had

sourceCompatibility = '11'

Changed this to 8 and it stopped throwing 'compile failed invalid release 11' error

solution reference: github forum

veritas
  • 378
  • 1
  • 6
  • 16
1

Add this with maven compler plugin

<dependencies>
...
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Harsh
  • 2,893
  • 29
  • 16
1

Removing maven pom properties:

<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

Solved it for me (so not setting it to 1.8 as suggested above)

Hmbucker
  • 115
  • 2
  • 10
0

I got the same java 11 target error and my issue was maven-compiler-plugin outdated version. upgraded to lastest and everything worked

Adrian
  • 29
  • 1
0

Setting the right path for Java home in gradle.properties solved the issue for me

gradle properties path : C:\Users<username>.gradle\gradle.properties

org.gradle.java.home=yourjdk11homepath
Example : org.gradle.java.home=C\:/Program Files/Java/jdk-11.0.8
jfk
  • 4,335
  • 34
  • 27
0

If configuring the Project Properties never worked for you, you should also remember to change the SDK version for the maven or Gradle which could be pointing to the wrong path or the JDK version.

To do this:

  1. Click on File | Settings | Build, Execution, Deployment | Build Tools
  2. If maven, click maven then Runner, and set JRE to use project JDK.
  3. If Gradle, Click Gradle then set the Gradle JVM.
David Kariuki
  • 1,522
  • 1
  • 15
  • 30
0

Ok, this question is pretty old and probably not relevant for the most of the developers, but I would like to show the working solution to this problem.

First - my understanding of the problem: Normally (by default) IntelliJ uses it's internal compiler to compile the sources. Probably there's a way to specify the compiler, but I still haven't found the way to configure this. THIS IS JUST A GUESS: maybe IntelliJ tries to use the project JDK compiler and if this fails - uses the internal. In my case the IDEA is old version and the internal JRE (and compiler) is version 8. Of course this compiler won't be able to compile Java 11 sources - it issues error "java: invalid target release: 11"

The solution: To make sure that JDK compiler is used - we must delegate the build job to Maven. Maven will use the proper javac executable and it will be able to compile Java 11 sources to whatever target version you want.

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Runner then check "Delegate IDE build/run actions to maven"

That's it - now your old IntelliJ IDEA will be able to build/run projects with any new JDK version.

DVD
  • 31
  • 3