Java Path resolve() Method

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

Definition

In Java, dir.resolve(other) joins other onto dir with the correct separator: Paths.get("/home/anna").resolve("docs/report.txt") is /home/anna/docs/report.txt. It is the safe replacement for gluing strings together with "/".

Two special cases: if other is absolute, it is returned as it is, and if other is empty, dir itself is returned. Like all Path methods, resolve does not touch the disk and does not clean up . or ...

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

Syntax

Pass the child as a String or a Path; the original path does not change:

Java
Path home = Paths.get("/home/anna");
Path report = home.resolve("docs/report.txt");        // /home/anna/docs/report.txt
Path song = home.resolve(Paths.get("music", "a.mp3")); // /home/anna/music/a.mp3
CallWhat it does
dir.resolve("name.txt")A file inside dir.
dir.resolve("a").resolve("b.txt")Chain calls to go deeper.
dir.resolve(userInput).normalize()Join, then remove .. before checking it.

Since Java 7; the String overload is a default method from Java 11. Java 22 added varargs overloads, such as resolve("a", "b.txt"); on older versions chain the calls.

Library declarations
Java
// java.nio.file.Path (interface)
Path resolve(Path other)
Path resolve(String other)
// Java 22+:
Path resolve(Path first, Path... more)
Path resolve(String first, String... more)

Parameters

ParameterDescription
otherThe path to join on. A relative path is appended; an absolute path replaces the result; an empty path changes nothing.

Return Value

Return ValueDescription
PathThe combined path, or other when it is absolute, or this path when other is empty.

Compare with Similar Methods

Methods that build paths:

MethodHow it differs
resolveSibling()Joins onto the parent, replacing the last name.
relativize()The opposite: a.resolve(a.relativize(b)) gives back b.
normalize()Cleans up the .. that resolve keeps.

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 — Join Different Children onto a Home Folder

Resolve a folder name, a relative file path, a Path object, an absolute path, and an empty string.

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

public class PathResolveBasic {
    public static void main(String[] args) {
        Path home = Paths.get("/home/anna");
        System.out.println("docs            -> " + home.resolve("docs"));
        System.out.println("docs/report.txt -> " + home.resolve("docs/report.txt"));
        System.out.println("music, song.mp3 -> " + home.resolve(Paths.get("music", "song.mp3")));
        System.out.println("/etc/hosts      -> " + home.resolve("/etc/hosts"));
        System.out.println("(empty)         -> " + home.resolve(""));
    }
}

How It Works

  1. Relative children are appended. The first three calls add one or more names after /home/anna; the String and Path overloads behave the same.
  2. An absolute child wins. /etc/hosts already starts at the root, so resolve returns it unchanged and the home folder is ignored completely.
  3. Empty means "stay here". Resolving the empty string adds no names, so the result is the home path itself.

Example 2 — Build the Folder Layout of an Application

Derive config, log, and cache locations from one application folder. The base path stays the same after every call.

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

public class PathResolveAppTree {
    public static void main(String[] args) {
        Path app = Paths.get("myapp");
        Path config = app.resolve("config");
        Path settings = config.resolve("settings.properties");
        Path log = app.resolve("logs").resolve("2026-11.log");
        Path cache = app.resolve(Paths.get("cache", "images"));
        System.out.println("settings: " + settings);
        System.out.println("log:      " + log);
        System.out.println("cache:    " + cache);
        System.out.println("app is still: " + app);
    }
}

How It Works

  1. One base, many locations. Every path is derived from app, so moving the application means changing a single string.
  2. Chain for depth. app.resolve("logs").resolve("2026-11.log") goes two levels down; on Java 8 to 21 chaining is the way to add several names one by one.
  3. Immutable base. resolve returns new objects, so app still prints myapp at the end; nothing was created on disk either, which Files.createDirectories would do.

Example 3 — See That resolve() Keeps ".." Until You Normalize

Join a child that climbs out of the folder, clean it with normalize(), and check whether it is still inside the reports folder.

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

public class PathResolveDotsKept {
    public static void main(String[] args) {
        Path reports = Paths.get("company/reports");
        Path joined = reports.resolve("../secrets/salaries.csv");
        System.out.println("resolved:   " + joined);
        System.out.println("normalized: " + joined.normalize());
        System.out.println("inside reports? " + joined.normalize().startsWith(reports));
        Path a = reports.resolve("2026/q1.csv");
        Path b = reports.resolve(Paths.get("2026", "q1.csv"));
        System.out.println("String and Path overloads equal: " + a.equals(b));
    }
}

How It Works

  1. Glue without judging. resolve appends ../secrets/salaries.csv exactly as written, so the joined path still contains the .. step.
  2. Cleaning reveals the target. normalize() cancels reports against .., showing that the file is really in company/secrets, outside the reports folder.
  3. Two overloads, one result. A String with a slash and a Path built from parts give equal paths, so choose whichever reads better.

Frequently Asked Questions

Call base.resolve(child), for example Paths.get("/home/anna").resolve("docs/a.txt"). It inserts the right separator for the operating system.
It returns the absolute argument unchanged and ignores the base. Check user input for this case.
No. It only joins the paths. Call normalize() afterwards to resolve "." and "..".
resolve joins onto the path itself, so a/b.txt plus c.txt becomes a/b.txt/c.txt. resolveSibling joins onto the parent, giving a/c.txt.
Java 22 added resolve(first, more...). On older versions chain the calls, such as dir.resolve("a").resolve("b.txt"), or pass "a/b.txt".

Next: Path.resolveSibling()

Replace the last name of a path, keeping the same parent folder.

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

12 people found this page helpful