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;.
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.
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));
}
}
Output
Run with java PathToAbsoluteBasic. Shown for Linux and macOS. On Windows the second line prints reports\jan.txt; the other lines are the same in any working directory.
starts with working dir: true
relative to working dir: reports/jan.txt
file name: jan.txt
two names longer: true
How It Works
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.
Working directory in front. The absolute report path begins with cwd, and relativizing it back leaves exactly the two names that were typed.
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.
Run with java PathToAbsoluteAlready. Shown for Linux and macOS. On Windows the first two lines print false, because \srv\data\sales.csv has no drive letter and gets one added; the last two lines are the same.
already absolute: true
returned unchanged: true
relative changed: true
same file name: true
How It Works
Nothing to add. /srv/data/sales.csv already starts at the root on Linux and macOS, so toAbsolutePath() returns an equal path.
Relative paths grow. sales.csv gets the working directory in front, so the result is no longer equal to the original.
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"));
}
}
Output
Run with java PathToAbsoluteDots. The output is the same on Windows, macOS, and Linux.
has .. after toAbsolutePath: true
has .. after normalize: false
ends with shared/a.txt: true
How It Works
Prepend, nothing more.toAbsolutePath() places the working directory in front of ../shared/a.txt and keeps the .. element as it is.
Normalize finishes the job. In an absolute path every .. can be resolved (at worst against the root), so after normalize() none is left.
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.