java 尝试使用此语言级别不支持的资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38965221/
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-11-03 03:57:59  来源:igfitidea点击:

Try With Resources Not Supported at This Language Level

javaintellij-ideagradletry-with-resources

提问by Calicoder

I'm using IntelliJ IDEA Ultimate 2016.2.1, have set Project SDK to my 1.8 version, Project Language Level to 8, Module SDK to my 1.8 version, and JDK home path to /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home.

我正在使用 IntelliJ IDEA Ultimate 2016.2.1,已将项目 SDK 设置为我的 1.8 版本,将项目语言级别设置为 8,将模块 SDK 设置为我的 1.8 版本,并将 JDK 主路径设置为 /Library/Java/JavaVirtualMachines/jdk1.8.0_91。 jdk/目录/主页。

I have restarted the IDE.

我已经重新启动了 IDE。

Still, I am getting ugly warnings about try-with-resources not supported at this language level. I have not used less than language level 8 in IntelliJ so this can't be due to something not refreshing. I am using a gradle build configuration - cleaned built and tried to run - says I'm running 1.5 but I can't figure out where 1.5 is mentioned. In build.gradle i'm using:

尽管如此,我还是收到了有关此语言级别不支持的 try-with-resources 的丑陋警告。我在 IntelliJ 中使用的语言级别不低于 8,所以这不可能是由于某些东西不令人耳目一新。我正在使用 gradle 构建配置 - 清理构建并尝试运行 - 说我正在运行 1.5 但我不知道在哪里提到了 1.5。在 build.gradle 我使用:

 apply plugin: 'java'
 apply plugin: 'application'
 sourceCompatibility = 1.8

Should I file an IntelliJ bug or am I still missing something?

我应该提交 IntelliJ 错误还是我仍然遗漏了什么?

EDIT:

编辑:

Running gradle in termianl outside of IntelliJ's system runs it fine. So the problem is IntelliJ related.

在 IntelliJ 系统之外的终端中运行 gradle 运行良好。所以问题是 IntelliJ 相关的。

回答by Aaron Davis

I have found that IntelliJ sometimes defaults modules to language levels that don't match the set project SDK. Try right clicking on the module in the project explorer and select module settings and then the language level tab. If that is set to Java 5 then update it to Java 8.

我发现 IntelliJ 有时会将模块默认为与设置的项目 SDK 不匹配的语言级别。尝试在项目资源管理器中右键单击模块并选择模块设置,然后选择语言级别选项卡。如果将其设置为 Java 5,则将其更新为 Java 8。

回答by YouAreAwesome

Hey @Calicoder just try to make change your Java compiler version into 1.7 or 1.8 as mentioned below

嘿@Calicder 只是尝试将您的 Java 编译器版本更改为 1.7 或 1.8,如下所述

enter image description here

在此处输入图片说明

Thanks hope this will help.

谢谢希望这会有所帮助。

回答by ayahuasca

Make sure your try-with-resources is using the correct semantics. For instance, this:

确保您的 try-with-resources 使用正确的语义。例如,这个:

try (new BufferedReader(request.getReader())) {

}

will produce that same error message.

会产生同样的错误信息。