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
Call
What 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.
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());
}
}
}
Output
Run with java PathIsAbsoluteBasic. Shown for Linux and macOS. On Windows the first and last lines print false, because a path that starts with / but has no drive letter is not absolute there.
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.
Dots do not make a path absolute../notes.txt and ../notes.txt still depend on where the program runs, exactly like plain notes.txt.
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);
}
}
}
Output
Run with java PathIsAbsoluteAnchor. Shown for Linux and macOS. On Windows no entry has a drive letter, so none counts as absolute and the paths print with \; use entries such as C:\logs\app.log there.
Decide per entry. The conditional keeps the path unchanged when isAbsolute() is true and otherwise joins it onto appHome.
Relative entries move under the base. logs/app.log and data/users.csv become full paths inside /srv/app, independent of the working directory.
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.
Run with java PathIsAbsoluteComplete. The output is the same on Windows, macOS, and Linux for any working directory other than the file-system root.
rel.isAbsolute(): false
abs.isAbsolute(): true
abs.endsWith(rel): true
more names in abs: true
How It Works
Relative first. reports/jan.txt has no root, so the first check is false on every system.
The working directory is put in front.toAbsolutePath() prepends the current folder (with a drive on Windows), and the result passes isAbsolute().
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.