Java Path toAbsolutePath() Method

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

Definition

In Java, path.toAbsolutePath() returns an absolute version of the path. If the path is relative, the current working directory of the program (the system property user.dir) is put in front: reports/jan.txt run from /home/anna/shop becomes /home/anna/shop/reports/jan.txt. An absolute path is returned as it is.

The method does not check whether the file exists, does not follow links, and does not remove . or ... It is the quick way to see where a relative path would point.

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

Syntax

Call it with no arguments:

Java
Path rel = Paths.get("reports", "jan.txt");
Path abs = rel.toAbsolutePath();          // <working directory>/reports/jan.txt
Path cwd = Paths.get("").toAbsolutePath(); // the working directory itself
CallWhat it does
p.toAbsolutePath()Working directory + p; unchanged if p is absolute.
Paths.get("").toAbsolutePath()The working directory as a Path.
p.toAbsolutePath().normalize()Absolute and without ./.., still no disk access.

Since Java 7. The working directory is fixed when the JVM starts; it is wherever the program was launched from, which may differ between an IDE and a terminal.

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

Parameters

ParameterDescription
(none)No arguments.

Return Value

Return ValueDescription
PathAn absolute path that locates the same file as this path.

Compare with Similar Methods

Ways to complete a path:

MethodHow it differs
toRealPath()Also resolves links and .., but the file must exist.
base.resolve(p)Anchors p to a folder you choose instead of the working directory.
isAbsolute()Tells whether the conversion would change anything.

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 Out Where a Relative Path Points

Convert a relative report path and relate it to the working directory, without printing the directory itself.

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

public class PathToAbsoluteBasic {
    public static void main(String[] args) {
        Path cwd = Paths.get("").toAbsolutePath();
        Path abs = Paths.get("reports", "jan.txt").toAbsolutePath();
        System.out.println("starts with working dir: " + abs.startsWith(cwd));
        System.out.println("relative to working dir: " + cwd.relativize(abs));
        System.out.println("file name:               " + abs.getFileName());
        System.out.println("two names longer:        " + (abs.getNameCount() == cwd.getNameCount() + 2));
    }
}

How It Works

  1. The empty path is the working directory. Paths.get("").toAbsolutePath() resolves nothing onto the working directory, so cwd is that directory as an absolute Path.
  2. Working directory in front. The absolute report path begins with cwd, and relativizing it back leaves exactly the two names that were typed.
  3. Relations instead of raw text. The program prints only facts about the paths (a prefix check, the relative rest, the name, the count) because the directory itself differs on every machine.

Example 2 — Leave Absolute Paths Unchanged

An absolute path comes back unchanged, while a relative one becomes a different path. On Windows a path without a drive letter behaves like a relative one.

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

public class PathToAbsoluteAlready {
    public static void main(String[] args) {
        Path fixed = Paths.get("/srv/data/sales.csv");
        System.out.println("already absolute:   " + fixed.isAbsolute());
        System.out.println("returned unchanged: " + fixed.toAbsolutePath().equals(fixed));
        Path rel = Paths.get("sales.csv");
        System.out.println("relative changed:   " + !rel.toAbsolutePath().equals(rel));
        System.out.println("same file name:     " + rel.toAbsolutePath().getFileName().equals(rel));
    }
}

How It Works

  1. Nothing to add. /srv/data/sales.csv already starts at the root on Linux and macOS, so toAbsolutePath() returns an equal path.
  2. Relative paths grow. sales.csv gets the working directory in front, so the result is no longer equal to the original.
  3. The name survives. The last element of the absolute path is still sales.csv, so comparing getFileName() with the original one-name path is true.

Example 3 — See That toAbsolutePath() Keeps ".."

Convert a path that starts with .. and look for the .. element before and after normalize().

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

public class PathToAbsoluteDots {
    static boolean hasDotDot(Path p) {
        for (Path part : p) {
            if (part.toString().equals("..")) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Path abs = Paths.get("../shared/a.txt").toAbsolutePath();
        System.out.println("has .. after toAbsolutePath: " + hasDotDot(abs));
        System.out.println("has .. after normalize:      " + hasDotDot(abs.normalize()));
        System.out.println("ends with shared/a.txt:      " + abs.normalize().endsWith("shared/a.txt"));
    }
}

How It Works

  1. Prepend, nothing more. toAbsolutePath() places the working directory in front of ../shared/a.txt and keeps the .. element as it is.
  2. Normalize finishes the job. In an absolute path every .. can be resolved (at worst against the root), so after normalize() none is left.
  3. Still no disk access. The cleaned path ends with shared/a.txt whether or not such a file exists; toRealPath() would be the call that checks.

Frequently Asked Questions

Call path.toAbsolutePath(). It puts the working directory in front of a relative path; add normalize() to remove "." and "..".
The folder the JVM was started from, available as the system property user.dir and as Paths.get("").toAbsolutePath().
No. It only builds a longer path. Use Files.exists or toRealPath to involve the disk.
toAbsolutePath only adds the working directory. toRealPath also resolves "..", follows symbolic links, returns the real case of names on some systems, and throws if the file is missing.
IDEs often start programs from the project folder, while a terminal uses its current folder. Resolve paths against a known base folder when the location matters.

Next: Path.toFile()

Convert a Path into a java.io.File for older APIs.

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

7 people found this page helpful