简体   繁体   English

使用Try-Catch with Resources时,IntelliJ IDE会出错

[英]IntelliJ IDE gives error when using Try-Catch with Resources

I am attempting to use JDK 7's "try-catch with resources" statement; 我试图使用JDK 7的“try-catch with resources”声明; IntelliJ highlights my resource line, saying IntelliJ强调我的资源线,说

Try-with-resources are not supported at this language level. 此语言级别不支持尝试使用资源。

When I try to compile, I get: 当我尝试编译时,我得到:

java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources) java:在-source 1.6中不支持try-with-resources(使用-source 7或更高版本来启用try-with-resources)

I checked that try-with-resources is enabled for my current project, and that my project is using JDK 7 (Library: C:\\Program Files\\Java\\jdk1.7.0_11). 我检查了我的当前项目是否启用了try-with-resources,我的项目是使用JDK 7(Library:C:\\ Program Files \\ Java \\ jdk1.7.0_11)。 Any ideas? 有任何想法吗? I can't figure out what option to change (if that's even the issue). 我无法弄清楚要改变什么选项(如果这甚至是问题)。

Click on the File menu, open Project Structure, then under "Settings" there should be "Project". 单击文件菜单,打开项目结构,然后在“设置”下应该有“项目”。 Within that tab, there'll be an SDK Settings option which specifies the language version you want to use. 在该选项卡中,将有一个SDK设置选项,指定您要使用的语言版本。

See the JetBrains help page for more details ("Project language level"). 有关详细信息,请参阅JetBrains帮助页面 (“项目语言级别”)。

The only way this error will occur is if your module's language level isn't set to 1.7+. 发生此错误的唯一方法是,如果模块的语言级别未设置为1.7+。 This needs to be set in either your IntelliJ project/module settings, the project's pom.xml file, or both. 这需要在IntelliJ项目/模块设置,项目的pom.xml文件或两者中设置。

IntelliJ 的IntelliJ

在此输入图像描述

Maven Maven的

<properties>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>

Module settings can override project settings; 模块设置可以覆盖项目设置; if setting this at the project level and you have a specific issue in a module, check the module settings as well. 如果在项目级别设置此项并且模块中存在特定问题,请同时检查模块设置。

Besides mentioned instructions I also had to specify language level per module as well. 除了提到的说明,我还必须指定每个模块的语言级别。 File -> Project Structure -> Modules 文件 - >项目结构 - >模块

Also check your code. 还要检查你的代码。 You might have accidentally did something like this: 你可能不小心做了这样的事情:

try (HttpClients.createMinimal().execute(new HttpGet(String.format(
          "http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
          runningPort)))) {

instead of 代替

try (CloseableHttpResponse response = HttpClients.createMinimal().execute(new HttpGet(String.format(
          "http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
          runningPort)))) {

easy mistake to make when you don't intend on using the result of your closeable resource. 当您不打算使用可关闭资源的结果时,很容易犯错。 yet it will have that misleading error. 但它会产生误导性的错误。

Pictorial representation of module settings. 模块设置的图形表示。 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM