How do you fix java.nio.file.NoSuchFileException?

← Java answers

The path you gave does not exist from the program's working directory. Print path.toAbsolutePath() to see what Java is really looking for, then fix the path or create the file.

Path path = Path.of("data/config.json");
System.out.println(path.toAbsolutePath());   // where Java is really looking
System.out.println(Files.exists(path));
 
// Writing: create the parent folders first
Files.createDirectories(path.getParent());
Files.writeString(path, "{}");
  • Relative paths start at the working directory, which is the project root in many IDEs but a different folder when you run a JAR or a build tool.
  • Files inside your project's resources are on the classpath, not the file system, and cannot be opened with a path once packaged.
  • Check spelling and case, since some systems are case-sensitive.
  • The exception message names the missing path; if the missing part is a folder, create it before writing.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required