Error:java: invalid source release: 8 in Intellij.

2019-01-07 02:25发布

Im trying to compile some code in I'm using Intellij Ultimate 13.1.4, but I get the following error and I have no idea what it means:

Information:Using javac 1.7.0_55 to compile java sources
Information:java: Errors occurred while compiling module 'Example'
Information:Compilation completed with 1 error and 0 warnings in 3 sec
Information:1 error
Information:0 warnings
Error:java: invalid source release: 8

My guess is that its something related to Java 8 vs Java 7, but I have no idea what specifically. I've tried to Google around for this message, but they either talk about javac or target release, so it doesn't exactly seem to apply.

10条回答
劳资没心,怎么记你
2楼-- · 2019-01-07 02:44

I had the same issue when "downgrading" a project from Java 8 to Java 6. The reason was that it was not changed at all places in IntelliJ.

In IntelliJ 13.1.4 I had to change Java and SDK version on the following places not to get this error:

  • File -> Project Structure -> Project Settings
  • File -> Project Structure -> Module Settings -> Tab: Sources: Language Level
  • File -> Project Structure -> Module Settings -> Tab: Dependencies: Module SDK
  • File -> Settings -> Compiler -> Java Compiler -> Target bytecode version

screen shot of File > Project Structure > Project

screen shot of File > Project Structure > Modules > Sources

screen shot of File > Project Structure > Modules > Dependencies

screen shot of File > Settings/Preferences > Compiler > Java Compiler

The last bullet was the one that was not updated in my case. Once I changed this, the error disappeared.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-07 02:46

Andreas Lundgren's answer worked and I was able to compile and run my app.

However, when I tried to run the project's associated JUnit tests I received the same error. Running

gradle -version

from Windows command prompt showed that gradle was still picking up the incorrect jdk. To fix it I had to set the JAVA_HOME environment variable to point to the correct jdk and restart IntelliJ.

查看更多
迷人小祖宗
4楼-- · 2019-01-07 02:50

It can be simply overcome by setting on Project Structure. You just need to select the right path for related version of JDK. Select new on dependencies tab, and choose the path. It's done!

enter image description here

查看更多
来,给爷笑一个
5楼-- · 2019-01-07 02:51

For Gradle users having this issues, if nothing above helps this is what solved my problem - apply this declarations in your build.gradle files:

targetCompatibility = 1.6 //or 1.7;1.8 and so on
sourceCompatibility = 1.6 //or 1.7;1.8 and so on

Problem solved!

查看更多
Evening l夕情丶
6楼-- · 2019-01-07 03:00

You need to click to the project Open Module Settings and change the path of your JDK, if in the file POM you use jdk 1.8, configure jdk 1.8 with correct path.

查看更多
甜甜的少女心
7楼-- · 2019-01-07 03:01

If you are using Gradle as a build tool and you get this error when executing a Gradle task i.e TomcatRun take a look to my other answer to the same question

javac: invalid target release: 1.8

查看更多
登录 后发表回答