Java Path relativize() Method

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

Definition

In Java, base.relativize(target) returns a relative path that, resolved against base, leads to target. From /home/anna/docs to /home/anna/music/song.mp3 the answer is ../music/song.mp3: one step up, then down into music.

It is the inverse of resolve: for normalized paths, base.resolve(base.relativize(target)) equals target. It works on names only; neither path has to exist.

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

Syntax

Call it on the starting point and pass the destination:

Java
Path docs = Paths.get("/home/anna/docs");
Path song = Paths.get("/home/anna/music/song.mp3");
Path rel = docs.relativize(song);          // ../music/song.mp3
CallWhat it does
base.relativize(child)The part of child below base, e.g. docs/a.txt.
a.relativize(b)Uses .. to climb out of a when needed.
p.relativize(p)The empty path.

Since Java 7. Both paths must be of the same type: both absolute or both relative (and on Windows, on the same drive).

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

Parameters

ParameterDescription
otherThe destination path, of the same type and file system as this path.

Return Value

Return ValueDescription
PathA relative path from this path to other; the empty path when both are equal.

Compare with Similar Methods

Related methods:

MethodHow it differs
resolve()The inverse operation: base plus relative path gives the target.
subpath()Cuts by index; cannot climb up with ...
startsWith()Tells whether the result will be free of ...

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 — Compute Relative Paths in a Home Folder

Go from a folder down to a file, sideways into another folder, up from a file, and from a path to itself.

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

public class PathRelativizeBasic {
    public static void main(String[] args) {
        Path home = Paths.get("/home/anna");
        Path docs = Paths.get("/home/anna/docs");
        Path report = Paths.get("/home/anna/docs/report.txt");
        Path song = Paths.get("/home/anna/music/song.mp3");
        System.out.println("home -> report: " + home.relativize(report));
        System.out.println("docs -> song:   " + docs.relativize(song));
        System.out.println("report -> home: " + report.relativize(home));
        System.out.println("docs -> docs:   [" + docs.relativize(docs) + "]");
    }
}

How It Works

  1. Down is the easy case. report starts with home, so the answer is just the remaining names, docs/report.txt.
  2. Up, then down. docs and song share /home/anna; one .. leaves docs, then music/song.mp3 goes down to the target.
  3. Counting steps up. From report back to home two names must be left (report.txt and docs), so the result is ../..; a path relativized against itself is empty, shown as [].

Example 3 — Fix the "Different Type of Path" Error

Relativizing a relative path against an absolute one fails. Make both absolute, and check the round trip with resolve.

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

public class PathRelativizeTypes {
    public static void main(String[] args) {
        Path docs = Paths.get("docs");
        try {
            docs.relativize(Paths.get("/home/anna/docs/a.txt"));
        } catch (IllegalArgumentException e) {
            System.out.println("relative vs absolute -> " + e.getClass().getSimpleName());
        }
        Path fixed = docs.toAbsolutePath().relativize(Paths.get("docs", "a.txt").toAbsolutePath());
        System.out.println("both absolute: " + fixed);
        Path target = Paths.get("docs/2026/a.txt");
        System.out.println("round trip: " + docs.resolve(docs.relativize(target)));
    }
}

How It Works

  1. Same kind required. docs is relative and the other path is absolute, so Java cannot know how they connect and throws IllegalArgumentException.
  2. Anchor both to the working directory. After toAbsolutePath() both paths start with the same folder, and the difference is just a.txt; only that relative result is printed.
  3. relativize and resolve undo each other. Resolving docs.relativize(target) back onto docs gives docs/2026/a.txt again.

Frequently Asked Questions

Call from.relativize(to). For /home/anna/docs and /home/anna/music/a.mp3 it returns ../music/a.mp3.
One path is absolute and the other is relative, or on Windows they are on different drives. Convert both with toAbsolutePath() first.
No. It only compares name elements, so the paths do not have to exist and symbolic links are not followed.
resolve. For normalized paths, base.resolve(base.relativize(target)) equals target.
Call toString() and replace backslashes with forward slashes, as Windows uses \ as its separator.

Next: Path.resolve()

Join a child name or relative path onto a folder path.

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

11 people found this page helpful