Java Path isAbsolute() Method

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

Definition

In Java, path.isAbsolute() returns true when the path contains everything needed to find the file, without a working directory. On Linux and macOS that means it starts with /; on Windows it needs a drive and a backslash, such as C:\data, or a network share.

A relative path such as docs/report.txt or ../shared/a.txt returns false. When a relative path is opened, Java reads it from the current working directory, which can differ between runs.

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

Syntax

Call it on a path; it never throws:

Java
boolean a = Paths.get("/home/anna/notes.txt").isAbsolute();   // true on Linux/macOS
boolean b = Paths.get("notes.txt").isAbsolute();             // false
CallWhat it does
p.isAbsolute()Is the path complete without a working directory?
p.toAbsolutePath().isAbsolute()Always true: the conversion completes the path.
p.isAbsolute() ? p : base.resolve(p)Keep absolute paths, anchor relative ones to a base folder.

Since Java 7. The answer comes from the text and the operating system's rules; the file does not need to exist.

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

Parameters

ParameterDescription
(none)No arguments.

Return Value

Return ValueDescription
trueThe path is absolute.
falseThe path is relative (it will be read from the working directory).

Compare with Similar Methods

Methods around absolute and relative paths:

MethodHow it differs
getRoot()Returns the root itself; on Windows a root alone does not make a path absolute.
toAbsolutePath()Turns a relative path into an absolute one.
resolve(Path)Joins a relative path onto a base; an absolute argument wins.

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 — Check Which Paths Are Absolute

Test an absolute file path, plain and dotted relative paths, and the root.

Java
import java.nio.file.Paths;

public class PathIsAbsoluteBasic {
    public static void main(String[] args) {
        String[] inputs = {"/home/anna/notes.txt", "notes.txt", "./notes.txt", "../notes.txt", "/"};
        for (String s : inputs) {
            System.out.println(s + " -> " + Paths.get(s).isAbsolute());
        }
    }
}

How It Works

  1. A leading slash is enough on Unix. On Linux and macOS a path that starts at the root / is absolute, so the first and last inputs print true.
  2. Dots do not make a path absolute. ./notes.txt and ../notes.txt still depend on where the program runs, exactly like plain notes.txt.
  3. Only text is inspected. No input needs to exist, and Java does not look at the disk to answer, so the check is fast and never throws.

Example 2 — Resolve Relative Config Entries Against an App Folder

Entries in a config file may be relative or absolute. Keep absolute ones and place relative ones inside the application folder.

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

public class PathIsAbsoluteAnchor {
    public static void main(String[] args) {
        Path appHome = Paths.get("/srv/app");
        String[] entries = {"logs/app.log", "/var/log/syslog", "data/users.csv"};
        for (String e : entries) {
            Path p = Paths.get(e);
            Path used = p.isAbsolute() ? p : appHome.resolve(p);
            System.out.println(e + " -> " + used);
        }
    }
}

How It Works

  1. Decide per entry. The conditional keeps the path unchanged when isAbsolute() is true and otherwise joins it onto appHome.
  2. Relative entries move under the base. logs/app.log and data/users.csv become full paths inside /srv/app, independent of the working directory.
  3. Absolute entries stay put. /var/log/syslog is printed as written; resolve would also return it unchanged, but the explicit check makes the intent obvious to readers.

Example 3 — Compare a Path Before and After toAbsolutePath()

A relative path becomes absolute after toAbsolutePath(), and still ends with the original names. Only booleans are printed, because the working directory differs on every computer.

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

public class PathIsAbsoluteComplete {
    public static void main(String[] args) {
        Path rel = Paths.get("reports", "jan.txt");
        Path abs = rel.toAbsolutePath();
        System.out.println("rel.isAbsolute():  " + rel.isAbsolute());
        System.out.println("abs.isAbsolute():  " + abs.isAbsolute());
        System.out.println("abs.endsWith(rel): " + abs.endsWith(rel));
        System.out.println("more names in abs: " + (abs.getNameCount() > rel.getNameCount()));
    }
}

How It Works

  1. Relative first. reports/jan.txt has no root, so the first check is false on every system.
  2. The working directory is put in front. toAbsolutePath() prepends the current folder (with a drive on Windows), and the result passes isAbsolute().
  3. Check, do not print. The original names are still at the end, which endsWith confirms; printing abs itself would show a folder that changes from machine to machine (if you run from /, the last line is the only one that could differ).

Frequently Asked Questions

Call path.isAbsolute(). It returns true for a complete path such as /home/anna/a.txt on Linux or C:\data\a.txt on Windows, and false for relative paths.
On Windows an absolute path needs a drive letter or a network share. A path that starts with a backslash or slash is only relative to the current drive.
Call path.toAbsolutePath(), which puts the current working directory in front. Use base.resolve(path) to anchor it to a folder of your choice instead.
No. It looks only at the form of the path. Use Files.exists(path) to check the disk.
To the working directory of the running Java program, usually the folder you started it from. Its value is in the system property user.dir.

Next: Path.iterator()

Walk through the name elements of a path one by one as Path objects.

iterator() 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