10

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
  • 890
  • 2
  • 7
  • 22

4 Answers4

16

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

Lindstorm
  • 890
  • 2
  • 7
  • 22
7

Files.readString(Path) was introduced in Java 11. It seems like your installation is still on Java 8.

First make sure your project uses Java 11 by setting the correct SDK and language level under Project Settings/Project.

If that doesn't work, make sure that you have installed the JDK 11 version of IntelliJ. You can choose between the JDK 8 and JDK 11 version on the IntelliJ download page when you click on the small downwards angle to the right of the download button on the IntelliJ download page.

Axel
  • 13,939
  • 5
  • 50
  • 79
1

The same with Lindstorm and Axel answers, the issue is due to a mis-configuration of you Java version. You probably build your source code using different source/target versions than JDK 11 (at least for Files::readString). I fix the issue using the above method.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>11</source>
        <target>11</target>
    </configuration>
</plugin>

I added in the maven plugin maven-compiler-plugin the configuration part. If you do not have this part (ex. you use spring boot run), add it in your pom.xml plugins area.

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
0

after changing the Project Bytecode Version as mentioned by @Lindstorm, intelliJ was still showing errors while compiling. I did the following to ensure the right (modified) config is picked.

  • invalidate the cache from files > invalidate cache.
  • close the project.
  • remove the project from intellij.
  • delete the .idea file from the root of the project.
  • open the project again.

this will ensure that the change of Project Bytecode Version to JDK 11 is picked by IntelliJ.

AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16