Java Path normalize() Method

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

Definition

In Java, path.normalize() returns a cleaned-up copy of the path: every . ("this folder") is removed, and every .. ("parent folder") is removed together with the name in front of it. docs/./old/../report.txt becomes docs/report.txt.

It is purely textual. Java does not look at the disk, so the files do not need to exist, and symbolic links are not followed. A .. that has nothing in front of it stays in a relative path (../a.txt) and is dropped after a root (/../a.txt becomes /a.txt).

An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.normalize(...), after import java.nio.file.Path;.

Syntax

Call it with no arguments; the original path is not changed:

Java
Path messy = Paths.get("docs/./old/../report.txt");
Path clean = messy.normalize();          // docs/report.txt
CallWhat it does
p.normalize()Remove . and resolvable ...
base.resolve(input).normalize()Join, then clean, before a startsWith(base) check.
a.normalize().equals(b.normalize())Compare two spellings of a path.

Since Java 7. An already clean path is returned as it is (possibly the same object).

Library declarations
Java
// java.nio.file.Path (interface)
Path normalize()

Parameters

ParameterDescription
(none)No arguments.

Return Value

Return ValueDescription
PathThe path without redundant name elements; the empty path if everything cancels out (for example a/..).

Compare with Similar Methods

Cleaning and resolving paths:

MethodHow it differs
toRealPath()Asks the disk: follows links and fails if the file is missing.
toAbsolutePath()Adds the working directory but keeps . and ...
resolve()Joins paths and leaves the .. for normalize to clean.

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 — Clean Up Paths with "." and ".."

Normalize five paths, from a typical messy relative path to cases where everything cancels out.

Java
import java.nio.file.Paths;

public class PathNormalizeBasic {
    public static void main(String[] args) {
        String[] inputs = {"docs/./reports/../report.txt", "./notes.txt", "../shared/a.txt",
                           "/home/anna/../../..", "a/b/../.."};
        for (String s : inputs) {
            System.out.println(s + " -> [" + Paths.get(s).normalize() + "]");
        }
    }
}

How It Works

  1. Dots vanish, pairs cancel. In the first path the . is dropped and reports cancels against the .. after it, leaving docs/report.txt.
  2. A leading ".." has no partner. ../shared/a.txt points above the working directory; there is no name before .. to remove, so it is kept.
  3. The root is a hard floor. /home/anna/../../.. tries to climb three levels from two names, so it stops at /; the relative a/b/../.. cancels completely and becomes the empty path, printed as [].

Example 2 — Compare Paths That Are Written Differently

Three spellings of the same source file are not equal until they are normalized. The name count shows what was removed.

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

public class PathNormalizeCompare {
    public static void main(String[] args) {
        Path a = Paths.get("projects/shop/../shop/src/App.java");
        Path b = Paths.get("projects/./shop/src/App.java");
        Path c = Paths.get("projects/shop/src/App.java");
        System.out.println("a.equals(c): " + a.equals(c));
        System.out.println("b.equals(c): " + b.equals(c));
        System.out.println("a normalized: " + a.normalize());
        System.out.println("equal after normalize: " + (a.normalize().equals(c) && b.normalize().equals(c)));
        System.out.println("names in a: " + a.getNameCount() + " -> " + a.normalize().getNameCount());
    }
}

How It Works

  1. equals compares the text. a and b contain extra .. and . elements, so neither is equal to c, even though all three describe the same file.
  2. Normalize both sides. After normalize(), shop/.. disappears from a and the dot from b, and all three paths are equal.
  3. Two names fewer. a starts with six names; the cancelled pair shop and .. leaves four, which getNameCount() confirms.

Example 3 — See What Happens When ".." Climbs Too High

Relative paths keep the .. steps they cannot cancel; absolute paths cannot climb above the root.

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

public class PathNormalizeAbove {
    public static void main(String[] args) {
        String[] inputs = {"../a.txt", "docs/../../b.txt", "/../a.txt", "/srv/../../c.txt"};
        for (String s : inputs) {
            Path n = Paths.get(s).normalize();
            System.out.println(s + " -> " + n + " (absolute: " + n.isAbsolute() + ")");
        }
    }
}

How It Works

  1. Relative paths remember the climb. In docs/../../b.txt the first .. cancels docs and the second has no partner, so ../b.txt still points one level above the start.
  2. Above the root is the root. For /../a.txt and /srv/../../c.txt every extra .. after the root is simply dropped, because nothing exists above /.
  3. Leftover ".." is a warning sign. A normalized relative path that still starts with .. leaves its base folder; reject such input when it should stay inside a folder.

Frequently Asked Questions

It removes "." elements and removes each ".." together with the name in front of it. docs/./old/../a.txt becomes docs/a.txt.
No. It only works on the text of the path. Use toRealPath() to resolve the path against the disk.
In a relative path there is no name in front of it to remove, so the step up must be kept. After a root such as / it is removed, because nothing is above the root.
normalize is textual and never fails. toRealPath asks the file system, follows symbolic links, returns an absolute path, and throws IOException if the file does not exist.
It is one step. Resolve the user input against the base folder, normalize the result, and then check that it still startsWith the base. Use toRealPath for links.

Next: Path.of()

Create a Path from strings or a URI (Java 11+; the same as Paths.get).

of() 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.

9 people found this page helpful