Java Path getRoot() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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;.

Syntax

Call it on any path:

Java
Path root = Paths.get("/home/anna/notes.txt").getRoot();   // /
Path none = Paths.get("notes.txt").getRoot();              // null
CallWhat it does
p.getRoot()The root as a Path, or null.
p.getRoot() != nullThe 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

ParameterDescription
(none)No arguments; the root of the current path is returned.

Return Value

Return ValueDescription
PathThe root component, for example /, C:\, or \.
nullThe path is relative and has no root.

Compare with Similar Methods

Root-related methods:

MethodHow it differs
isAbsolute()A boolean: is the path complete without a working directory?
getParent()Returns the root for a path with one name, such as /home.
getNameCount()Counts the names; the root is never counted.

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 — 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());
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathRootStrip {
    public static void main(String[] args) {
        Path file = Paths.get("/home/anna/photos/cat.png");
        Path root = file.getRoot();
        Path viaRoot = root.relativize(file);
        Path viaSubpath = file.subpath(0, file.getNameCount());
        System.out.println("Entry name: " + viaRoot);
        System.out.println("Absolute?   " + viaRoot.isAbsolute());
        System.out.println("Same:       " + viaRoot.equals(viaSubpath));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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());
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: Path.isAbsolute()

Check whether a path is complete on its own or relative to the working directory.

isAbsolute() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful