50

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Yankee
  • 1,011
  • 1
  • 9
  • 18

5 Answers5

53

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
52

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • This is no longer valid in AS 0.3.4 and above. – Adam Nov 22 '13 at 03:26
  • 1
    @Adam Android Studio? The one based on the Intellij beta? What specifically isn't valid? Wouldn't it make more sense to provide actionable information, although nobody was talking about Android development? – Dave Newton Nov 22 '13 at 04:02
  • This was the reason I had this issue. – conorgriffin Nov 06 '14 at 22:01
  • 5
    I still had to make changes in the "Modules" tab, it can overwrite Language level from the "Project" tab. So if the problem remains, check the Language level settings in "Modules" tab. – chris544 Jun 03 '15 at 16:13
  • @chris544 That's a good point; I'll edit that into the answer. – Dave Newton Jun 03 '15 at 16:15
  • Ty for mentioning Maven...I was checking in my Intellij configurations and everything seemed ok – dragosb Mar 08 '16 at 10:16
  • If you are using Gradle check sourceCompatibility is set to 1.7 + – J11 Feb 07 '17 at 17:34
11

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

Ihor Tsebriy
  • 111
  • 1
  • 2
8

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.

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
3

Pictorial representation of module settings. enter image description here

CodeShadow
  • 3,503
  • 1
  • 17
  • 16