intelliJ cannot find a specific method

Lindstorm :

I am getting a compile error in the following code that I do not know how to fix.

 public class Test throws IOException{
      public static void main(String[] args) {
           String path = "document.txt";
           File file = new File(path);
           Files.readString(file.toPath()); //cannot find symbol method readString(java.nio.file.Path)
      }
 }

but I get

Error:(8, 14) java: cannot find symbol
symbol: method readString(java.nio.file.Path)
location: class java.nio.file.Files

There are a number of things to note.

  • This only happens for readString(Path) in java.nio.file.Files. If I were to try size(Path) (another method in java.nio.file.Files), it works
  • This code does not work in intelliJ but it works in eclipse

  • This code works if I create a new project in intelliJ but not in my current Maven project that I cloned from github

I have tried all suggestions here including:

  • Build > Rebuild Project
  • Recompiling just Test.java
  • File > Invalidate Caches
  • Checked the /src folder is marked as source folder
  • Reimporting Maven dependencies
Lindstorm :

As Axel's answer pointed out, the problem did have to do with the Java version, but it was not the SDK or language level.

What solved it was by going to File > Settings > Build > Compiler > Java Compiler. I then changed the Project Bytecode Version to 11 and removed Per Module Bytecode Version entries that were set to 10.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85210&siteId=1