错误:java:无效的源版本:Intellij 中的 8。这是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25878045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:23:58  来源:igfitidea点击:

Error:java: invalid source release: 8 in Intellij. What does it mean?

javaintellij-ideacompilationjavacintellij-13

提问by David says Reinstate Monica

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:

我正在尝试使用 Intellij Ultimate 13.1.4 编译一些代码,但出现以下错误,我不知道这意味着什么:

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 javacor target release, so it doesn't exactly seem to apply.

我的猜测是它与 Java 8 和 Java 7 相关,但我不知道具体是什么。我已经尝试在谷歌上搜索这条消息,但他们要么谈论javac要么target release,所以它似乎并不完全适用。

采纳答案by Andreas Lundgren

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.

在将项目从 Java 8“降级”到 Java 6 时,我遇到了同样的问题。原因是 IntelliJ 中的所有地方都没有更改。

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

在 IntelliJ 13.1.4 中,我必须在以下位置更改 Java 和 SDK 版本才不会出现此错误:

  • 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
  • 文件 -> 项目结构 -> 项目设置
  • 文件 -> 项目结构 -> 模块设置 -> 选项卡:来源:语言级别
  • 文件 -> 项目结构 -> 模块设置 -> 选项卡:依赖项:模块 SDK
  • 文件 -> 设置 -> 编译器 -> Java 编译器 -> 目标字节码版本

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

文件 >设置/首选项 >编译器 >Java 编译器的屏幕截图

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

最后一个项目符号在我的案例中没有更新。一旦我改变了这一点,错误就消失了。

回答by Yash Bajaj

Check your pom.xmlfirst (if you have one)
Check your module's JDKdependancy. Make sure that it is 1.8
To do this,go to Project Structure -> SDK's
Add the path to where you have stored 1.8(jdk1.8.0_45.jdkin my case)
Apply the changes
Now, go to Project Structure ->Modules
Change the Module SDK to 1.8
Apply the changes

检查您的pom.xml第一个(如果有)
检查您的模块的JDK依赖关系。确保它是 为了做到这一点,转到项目结构 -> SDK 的添加路径到您存储的位置1.8(在我的情况下) 应用更改 现在,转到项目结构 -> 模块更改模块 SDK 以 应用更改1.8

jdk1.8.0_45.jdk


1.8

Voila! You're done

瞧!你完成了

回答by juliangonzalez

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

如果您使用 Gradle 作为构建工具,并且在执行 Gradle 任务时出现此错误,即 TomcatRun,请查看我对同一问题的其他答案

javac: invalid target release: 1.8

javac:无效的目标版本:1.8

回答by jilbot

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

Andreas Lundgren 的回答有效,我能够编译和运行我的应用程序。

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

但是,当我尝试运行项目的关联 JUnit 测试时,我收到了同样的错误。跑步

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_HOMEenvironment variable to point to the correct jdk and restart IntelliJ.

从 Windows 命令提示符显示 gradle 仍在选择不正确的 jdk。为了修复它,我必须将JAVA_HOME环境变量设置为指向正确的 jdk 并重新启动 IntelliJ。

回答by Aziz Mamoyan

Change in pom.xml1.6to 1.8

更改pom.xml1.61.8

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

回答by Java_Waldi

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

对于遇到此问题的 Gradle 用户,如果以上没有任何帮助,这就是解决我的问题的方法 - 在 build.gradle 文件中应用此声明:

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

Problem solved!

问题解决了!

回答by B5A7

For Grails users, apply these declarations in your BuildConfig.groovy file:

对于 Grails 用户,请在 BuildConfig.groovy 文件中应用这些声明:

grails.project.target.level = 1.6 //or 1.7;1.8 and so on
grails.project.source.level = 1.6 //or 1.7;1.8 and so on

回答by snr

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

它可以通过设置简单地克服Project Structure。您只需要为相关版本的JDK选择正确的路径即可。选择new依赖关系选项卡,然后选择路径。完成!

enter image description here

在此处输入图片说明

回答by Katherine Nicol

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.

需要点击项目Open Module Settings,修改JDK的路径,如果在POM文件中使用的是jdk 1.8,请用正确的路径配置jdk 1.8。

回答by ArifMustafa

I checked all above said project version, module version, project bytecode version, target bytecode version settings in IntelliJ Idea, but all were the same as I scratched.

我检查了上面说的项目版本,模块版本,项目字节码版本,目标字节码版本设置IntelliJ Idea,但都和我抓的一样。

I face this error Error:java: invalid source release: 1.8in IntelliJ Idea 2017.2.6because I upgraded the dependency version Mavenpom file, which(dependency) were supposed to build for JDK 1.8 application and I were building my application on and with maven compiler source and target JDK 1.7.

我面临这个错误Error:java: invalid source release: 1.8IntelliJ Idea 2017.2.6因为我升级了依赖版本Mavenpom 文件,该文件(依赖)应该为 JDK 1.8 应用程序构建,并且我正在使用 maven 编译器源和目标 JDK 1.7 构建我的应用程序。

Hence I again downgrade the dependency version to earlier one in Mavenpom, and the error gone after project Rebuild Module 'xyz_project'.

因此,我再次将依赖版本降级到Mavenpom 中的较早版本,并且在项目重建模块“xyz_project”之后错误消失了。