Lindstorm
Lindstorm

Reputation: 889

intelliJ cannot find a specific method

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

String path = "document.txt";
File file = new File(path);
Files.readString(file.toPath()); //cannot find symbol method readString(java.nio.file.Path)

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.

I have tried all suggestions here including:

Upvotes: 10

Views: 12967

Answers (4)

Lindstorm
Lindstorm

Reputation: 889

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

Upvotes: 17

AppleCiderGuy
AppleCiderGuy

Reputation: 1297

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.

Upvotes: 0

Georgios Syngouroglou
Georgios Syngouroglou

Reputation: 19944

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.

Upvotes: 1

Axel
Axel

Reputation: 14159

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.

Upvotes: 8

Related Questions