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:
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.
Down is the easy case. report starts with home, so the answer is just the remaining names, docs/report.txt.
Up, then down. docs and song share /home/anna; one .. leaves docs, then music/song.mp3 goes down to the target.
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 2 — Build Relative Links Between Pages of a Static Site
A site generator needs links from a blog post to an image, a neighbour file, and the home page. Relativize from the folder of the page and switch to forward slashes for URLs.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathRelativizeLinks {
public static void main(String[] args) {
Path page = Paths.get("site/blog/2026/post.html");
Path folder = page.getParent();
Path[] targets = {Paths.get("site/images/logo.png"),
Paths.get("site/blog/2026/photo.jpg"),
Paths.get("site/index.html")};
for (Path t : targets) {
String link = folder.relativize(t).toString().replace('\\', '/');
System.out.println(t.getFileName() + " -> " + link);
}
}
}
Output
Run with java PathRelativizeLinks. The output is the same on Windows, macOS, and Linux.
Start from the folder. A browser resolves links against the folder of the page, so the code relativizes from page.getParent(), site/blog/2026, not from post.html itself.
Two levels up for shared files. The image and the home page are reached by leaving 2026 and blog, which gives the ../../ prefix.
URLs need forward slashes.replace('\\', '/') converts the Windows separator, so the generated links are identical on every system.
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.
Run with java PathRelativizeTypes. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
relative vs absolute -> IllegalArgumentException
both absolute: a.txt
round trip: docs/2026/a.txt
How It Works
Same kind required. docs is relative and the other path is absolute, so Java cannot know how they connect and throws IllegalArgumentException.
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.
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.