In Java, path.getRoot() returns the root component of a path as a Path, or null when the path has no root. On Linux and macOS the only root is /. On Windows a root can be a drive such as C:\, a network share such as \\server\share\, or a lone \.
The root is not a name element: /home/anna has the root / and two names, home and anna. Relative paths such as docs/a.txt have no root at all.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.getRoot(...), after import java.nio.file.Path;.
The path has a root (on Windows this is not the same as absolute).
p.getRoot().relativize(p)
The same names without the root, as a relative path.
Since Java 7. Roots are parsed by the file system of the running computer, so the same String can have a root on one system and none on another (Example 3).
Library declarations
Java
// java.nio.file.Path (interface)
Path getRoot()
Parameters
Parameter
Description
(none)
No arguments; the root of the current path is returned.
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 — Find the Root of Absolute and Relative Paths
Print the root of an absolute file path, a relative path, and the root itself, together with the name count.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathRootBasic {
public static void main(String[] args) {
String[] inputs = {"/home/anna/notes.txt", "notes.txt", "/"};
for (String s : inputs) {
Path p = Paths.get(s);
System.out.println(s + " -> root " + p.getRoot() + ", names " + p.getNameCount());
}
}
}
Output
Run with java PathRootBasic. Shown for Linux and macOS. On Windows the two roots print as \ instead of /; the name counts are the same.
The leading slash is the root. For /home/anna/notes.txt, getRoot() returns the one-character path /, and the three names after it are counted separately.
Relative means no root. notes.txt starts with a name, so the root is null; it will be read from the working directory when a file is opened.
A root with zero names. The path / is all root: getRoot() returns it unchanged and getNameCount() is 0.
Example 2 — Remove the Root to Store a Path Inside an Archive
Archive formats such as ZIP store relative entry names. Turn an absolute path into one in two ways and check that both agree.
Relativize against the root.root.relativize(file) asks "how do I get from / to the file?", and the answer is the four names without the leading slash.
A relative result. The entry name has no root any more, so isAbsolute() is false and it can be stored safely as an archive entry.
A second route.subpath(0, getNameCount()) copies all names but never the root, so it produces an equal path; use either, but check getRoot() for null first when the input may already be relative.
Example 3 — Parse a Windows-Style Path on Any System
The same String is split differently by each operating system. Run this on Linux and on Windows to see the difference.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathRootWindowsText {
public static void main(String[] args) {
Path p = Paths.get("C:\\data\\report.txt");
System.out.println("Root: " + p.getRoot());
System.out.println("Name count: " + p.getNameCount());
System.out.println("File name: " + p.getFileName());
}
}
Output
Run with java PathRootWindowsText. Shown for Linux and macOS. On Windows the program prints Root: C:\, Name count: 2, and File name: report.txt.
Root: null
Name count: 1
File name: C:\data\report.txt
How It Works
The Java literal."C:\\data\\report.txt" uses two backslashes in source code for each single backslash in the actual text, so the String is C:\data\report.txt.
Linux sees one long name. On Linux and macOS a backslash is an ordinary character, so the whole text becomes one relative name element with no root.
Windows sees a drive. The Windows file system recognizes C:\ as the root and data and report.txt as two names, which is why paths read from config files should be built for the system they run on.
Frequently Asked Questions
The root component of the path, such as / on Linux and macOS or C:\ on Windows, as a Path. It returns null when the path is relative.
On Linux and macOS, yes. On Windows, no: \data has the root \ but no drive, so isAbsolute() is false.
Use path.getRoot().relativize(path) or path.subpath(0, path.getNameCount()). Both give the names as a relative path.
Use FileSystems.getDefault().getRootDirectories(). It returns / on Linux and macOS and one root per drive, such as C:\ and D:\, on Windows.
No. Only the names after the root are counted, so / has a name count of 0.