11

Under File -> Project Structure -> SDKs i have 1.7 jdk but i still get this error:

added.java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources)

I have restarted IDE but to no avail

TastyCode
  • 5,679
  • 4
  • 37
  • 42

5 Answers5

23

Newer JVMs allow you to compile your code using compiler versions of lower JVM versions. For example, JDK 1.7 will allow you to compile your code using the 1.5 compiler.

See the "Cross compilation section" here http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

What you need to do in Intellij, is to go to File -> Project Structure -> Project and select the "Project SDK" as 1.7 and the "Project Language Level" as 7.0

Elmer
  • 809
  • 9
  • 15
Abbas Gadhia
  • 14,532
  • 10
  • 61
  • 73
8

Had to click on the 'error bulb' icon (or Alt+Enter) and select option to enable load multi-catch, try with resources, diamond ... and then reload and restart IDE.

TastyCode
  • 5,679
  • 4
  • 37
  • 42
  • That's strange that you had to manually enable try-with-resources should be automatic once you move to JDK-7, that's what I observed in eclipse. – Himanshu Bhardwaj Jun 14 '13 at 03:52
  • he probably had his language level set to 1.6 or lower, while using a 1.7 compiler. No need to restart the IDE, just change that setting. – jwenting Jun 14 '13 at 05:26
  • I had to do this manually as well. Updating the Project Structure settings was not enough. thanks. – isapir May 01 '14 at 03:40
  • Its just correct. Module level it was set to 5. Updated to Java 8 lambda... and it works like a charm. Thanks for pointing out. – Doogle Mar 26 '18 at 08:58
3

IntelliJ IDEA allows to set the language level on a project basis as well as on on module basis. If you have set the language level to Java 7 or higher on the project level, and are still getting this error message, right click on the module, select "Open module settings". Make sure the language level for the module is set to "Project default", or if for one reason, you want to set it specifically, it should be at least Java 7.

ocroquette
  • 3,049
  • 1
  • 23
  • 26
0

I had the same problem with IntelliJ 13. You have to set the language level, either for the entire project or individual modules, on the "General Settings for Project" dialog. See IntelliJ IDE gives error when using Try-Catch with Resources for the complete answer.

Community
  • 1
  • 1
Nick A. Watts
  • 819
  • 10
  • 16
0

If the above answers didn't help, are you using Groovy? Apparently Groovy doesn't support Java's try-with-resources in the current version.

pandoro
  • 3
  • 2