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

时间:2013-01-22 13:45:43

标签: java intellij-idea try-catch try-catch-finally try-with-resources

我正在尝试使用JDK 7的“try-catch with resources”语句; IntelliJ突出显示我的资源行,说

  

此语言级别不支持试用资源。

当我尝试编译时,我得到:

  

java:在-source 1.6中不支持try-with-resources    (使用-source 7或更高版本来启用try-with-resources)

我检查了为当前项目启用了try-with-resources,我的项目是使用JDK 7(Library:C:\ Program Files \ Java \ jdk1.7.0_11)。有任何想法吗?我无法弄清楚要改变什么选项(如果这甚至是问题)。

5 个答案:

答案 0 :(得分:51)

单击文件菜单,打开项目结构,然后在“设置”下应该有“项目”。在该选项卡中,将有一个SDK设置选项,用于指定您要使用的语言版本。

有关详细信息,请参阅JetBrains help page(“项目语言级别”)。

答案 1 :(得分:50)

此错误发生的唯一方法是模块的语言级别未设置为1.7+。这需要在IntelliJ项目/模块设置,项目的pom.xml文件或两者中设置。

<强>的IntelliJ

enter image description here

<强>的Maven

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

模块设置可以覆盖项目设置;如果在项目级别设置此项并且模块中存在特定问题,请同时检查模块设置。

答案 2 :(得分:8)

除了提到的说明,我还必须指定每个模块的语言级别。档案 - &gt;项目结构 - &gt;模块

答案 3 :(得分:7)

同时检查您的代码。你可能不小心做了这样的事情:

image_list = list(temp[0])
label_list = list(temp[1])

而不是

try (HttpClients.createMinimal().execute(new HttpGet(String.format(
          "http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
          runningPort)))) {
如果您不想使用可关闭资源的结果,那么很容易犯错误。但它会产生误导性的错误。

答案 4 :(得分:1)

模块设置的图示。 enter image description here

相关问题