Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intelliJ cannot find a specific method

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
like image 740
Lindstorm Avatar asked May 04 '19 05:05

Lindstorm


People also ask

Why is my class red in IntelliJ?

It means that the files exist locally, but are not in the repository, and are not scheduled for addition. With other words, the files are not under version control. There is not really a problem since the files can just be added to the VCS if desired.


1 Answers

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.

Note if this error keeps happening to you, this could be because the source and target version is not specified in your pom.xml. See this question for more details

like image 164
Lindstorm Avatar answered Oct 06 '22 00:10

Lindstorm