IntelliJ IDE gives error when using Try-Catch with Resources

JavaIntellij IdeaTry CatchTry Catch-FinallyTry with-Resources

Java Problem Overview


I am attempting to use JDK 7's "try-catch with resources" statement; IntelliJ highlights my resource line, saying

> 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)

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). Any ideas? I can't figure out what option to change (if that's even the issue).

Java Solutions


Solution 1 - Java

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.

See the JetBrains help page for more details ("Project language level").

Solution 2 - Java

The only way this error will occur is if your module's language level isn't set to 1.7+. This needs to be set in either your IntelliJ project/module settings, the project's pom.xml file, or both.

IntelliJ

enter image description here

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.

Solution 3 - Java

Besides mentioned instructions I also had to specify language level per module as well. File -> Project Structure -> Modules

Solution 4 - Java

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.

Solution 5 - Java

Pictorial representation of module settings. enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYankeeView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaDave NewtonView Answer on Stackoverflow
Solution 3 - JavaIhor TsebriyView Answer on Stackoverflow
Solution 4 - JavaNicholas DiPiazzaView Answer on Stackoverflow
Solution 5 - JavaCodeShadowView Answer on Stackoverflow