I am trying to copy and run the project, but I get an error: resource references are not supported at this language level.

In my settings, everything is set as follows:

enter image description here

I have already changed all the settings that I could, restarted the idea, but the error does not disappear.

  • one
    And the driver posgres to the project is attached? - keekkenen
  • try to delete iml file and open the project - Senior Pomidor
  • you lost the bracket ) - Bohdan Korinnyi
  • The driver works, as if just in try to insert the code, then the sql commands pass. - W8forme

1 answer 1

The try-with-resources construct works with resources that implement the AutoCloseable interface. This is stated in the documentation . The Class.forName method returns an object of type Class that does not implement this interface.

Accordingly, Class.forName cannot be used in try-with-resources .

Moreover, there is one more problem (although the first one is enough to create non-compiled code): you cannot just write, for example,

 try (new FileReader("file.txt")) 

Must be an assignment:

 try (FileReader reader = new FileReader("file.txt")) 

Class.forName code must be inside a try block:

 try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); }