Java编译报错:cannot find symbol(Files、Paths变量)求助
Hey there! Let's work through this compilation error you're facing. Those cannot find symbol messages for Files and Paths are totally normal when you're starting out with Java's file handling classes—this is a straightforward fix.
Why this error happens
The Files and Paths classes aren't part of Java's default imported classes (like java.lang which gets imported automatically). They live in the java.nio.file package, so the compiler doesn't recognize them unless you explicitly tell it where to find them.
Two easy solutions:
Option 1: Add import statements at the top of your code
Just include these lines right before your class declaration to pull in the required classes:import java.nio.file.Files; import java.nio.file.Paths; public class TestClass { // Your code here that uses Files and Paths public static void main(String[] args) { // Example usage: var path = Paths.get("test.txt"); // ... rest of your code } }If you plan to use other classes from
java.nio.file, you can also use a wildcard import to cover all of them:import java.nio.file.*;Option 2: Use fully qualified class names (no imports needed)
If you don't want to add import statements, you can write the full package path every time you use these classes. For example:public class TestClass { public static void main(String[] args) { var path = java.nio.file.Paths.get("test.txt"); boolean exists = java.nio.file.Files.exists(path); } }This works, but it's less clean than using imports—especially if you use these classes multiple times.
Once you apply either of these fixes, re-run javac TestClass.java and the errors should disappear. Keep pushing forward with your OCA studies—you're off to a great start!
内容的提问来源于stack exchange,提问作者Frank Moor

