In Java, path.toRealPath() asks the file system for the real location of an existing file or folder. The result is absolute, contains no . or .., follows symbolic links to their targets, and on case-insensitive systems may use the real letter case of the names.
Unlike most Path methods, it reads the disk, so the file must exist; otherwise it throws NoSuchFileException. It is the NIO.2 counterpart of File.getCanonicalPath().
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.toRealPath(...), after import java.nio.file.Path;.
Syntax
It declares throws IOException; pass LinkOption.NOFOLLOW_LINKS to keep links unresolved:
Absolute, cleaned, links followed; the file must exist.
p.toRealPath(LinkOption.NOFOLLOW_LINKS)
Same, but symbolic links are not replaced by their targets.
a.toRealPath().equals(b.toRealPath())
Do two spellings lead to the same file?
Since Java 7. Each example creates its own files in a fresh Files.createTempDirectory folder, prints only names and booleans, and deletes everything at the end.
Answers "same file?" directly, without returning a path.
Java Examples
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Resolve a Path with ".." to the Real File
Create a file and a sub-folder in a temp folder, then ask for the real path of a spelling that goes through the sub-folder and back.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class PathToRealPathClean {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path sub = Files.createDirectory(dir.resolve("sub"));
Path file = Files.createFile(dir.resolve("data.txt"));
Path real = dir.resolve("sub/../data.txt").toRealPath();
System.out.println("file name: " + real.getFileName());
System.out.println("absolute: " + real.isAbsolute());
System.out.println("already normal: " + real.normalize().equals(real));
System.out.println("same as file: " + real.equals(file.toRealPath()));
Files.delete(file);
Files.delete(sub);
Files.delete(dir);
}
}
Output
Run with java PathToRealPathClean. The output is the same on Windows, macOS, and Linux.
file name: data.txt
absolute: true
already normal: true
same as file: true
How It Works
Every step must exist. The detour sub/.. only works because sub was created first; the operating system walks the real folders, so a missing sub would cause NoSuchFileException.
A clean, absolute answer. The real path ends in data.txt, is absolute, and has no . or .. left, which is why normalizing it changes nothing.
Compare real with real. The check uses file.toRealPath(), not file: on macOS the temp folder is reached through a link, so only the two real paths are guaranteed to match.
Example 2 — Handle NoSuchFileException for a Missing File
Ask for the real path of a file that does not exist yet, then create it and ask again. toAbsolutePath() works in both cases.
Text-only methods do not care. report.txt does not exist yet, but toAbsolutePath() still returns an absolute path, because it never asks the disk.
toRealPath does care. Asking for the real location of a missing file throws NoSuchFileException; the program prints only the class name, since the message contains the temp path.
Create, then it works. After Files.createFile the same call succeeds, and the real path ends with report.txt.
Example 3 — Remove Duplicate Paths That Point to the Same File
Four spellings point to only two files. A TreeSet of the typed paths keeps all four; a set of real paths keeps two.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.TreeSet;
public class PathToRealPathDedupe {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path logs = Files.createDirectory(dir.resolve("logs"));
Path a = Files.createFile(dir.resolve("a.txt"));
Path b = Files.createFile(dir.resolve("b.txt"));
Path[] given = {a, dir.resolve("./a.txt"), logs.resolve("../a.txt"), b};
Set<Path> typed = new TreeSet<>();
Set<Path> real = new TreeSet<>();
for (Path p : given) {
typed.add(p);
real.add(p.toRealPath());
}
System.out.println("paths given: " + given.length);
System.out.println("distinct as typed: " + typed.size());
System.out.println("distinct real files: " + real.size());
for (Path r : real) {
System.out.println(" " + r.getFileName());
}
Files.delete(a);
Files.delete(b);
Files.delete(logs);
Files.delete(dir);
}
}
Output
Run with java PathToRealPathDedupe. The output is the same on Windows, macOS, and Linux.
paths given: 4
distinct as typed: 4
distinct real files: 2
a.txt
b.txt
How It Works
Different text, same file. a.txt, ./a.txt, and logs/../a.txt are three spellings of one file, so the TreeSet of typed paths, which compares text, still holds four entries.
Real paths collapse duplicates.toRealPath() turns all three spellings into the same absolute path, so the second set ends up with just a.txt and b.txt.
Sorted, short output. A TreeSet orders paths with compareTo, and printing only getFileName() keeps the random temp folder out of the output.
Frequently Asked Questions
It asks the file system for the real location of an existing file: an absolute path with "." and ".." resolved and symbolic links followed.
The file or one of the folders on the way does not exist. toRealPath needs a real file; use toAbsolutePath or normalize for paths that may not exist.
normalize only edits the text and never fails. toRealPath reads the disk, follows links, and fails for missing files.
They serve the same purpose. toRealPath returns a Path and throws specific exceptions such as NoSuchFileException, while getCanonicalPath returns a String and works for missing files too.