IntelliJ IDE gives error when using Try-Catch with Resources

前端 未结 5 644
栀梦
栀梦 2020-12-13 08:30

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

Try-with-resources are not supp

相关标签:
5条回答
  • 2020-12-13 09:02

    Pictorial representation of module settings.

    0 讨论(0)
  • 2020-12-13 09:11

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

    0 讨论(0)
  • 2020-12-13 09:14

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

    0 讨论(0)
  • 2020-12-13 09:14

    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.

    0 讨论(0)
  • 2020-12-13 09:23

    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.

    0 讨论(0)
提交回复
热议问题