Yan*_*kee 49 java try-catch intellij-idea 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).有任何想法吗?我无法弄清楚要改变什么选项(如果这甚至是问题).
Jon*_*eet 52
单击文件菜单,打开项目结构,然后在"设置"下应该有"项目".在该选项卡中,将有一个SDK设置选项,指定您要使用的语言版本.
有关详细信息,请参阅JetBrains帮助页面("项目语言级别").
Dav*_*ton 50
发生此错误的唯一方法是,如果模块的语言级别未设置为1.7+.这需要在IntelliJ项目/模块设置,项目pom.xml
文件或两者中设置.
的IntelliJ
Maven的
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
Run Code Online (Sandbox Code Playgroud)
模块设置可以覆盖项目设置; 如果在项目级别设置此项并且模块中存在特定问题,请同时检查模块设置.
还要检查你的代码.你可能不小心做了这样的事情:
try (HttpClients.createMinimal().execute(new HttpGet(String.format(
"http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
runningPort)))) {
Run Code Online (Sandbox Code Playgroud)
代替
try (CloseableHttpResponse response = HttpClients.createMinimal().execute(new HttpGet(String.format(
"http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
runningPort)))) {
Run Code Online (Sandbox Code Playgroud)
当您不打算使用可关闭资源的结果时,很容易犯错.但它会产生误导性的错误.