Reputation: 889
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.
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:
Test.java
Upvotes: 10
Views: 12967
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
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.
.idea
file from the root of the project.this will ensure that the change of Project Bytecode Version to JDK 11 is picked by IntelliJ.
Upvotes: 0
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
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