In Java, Path.of(first, more...) creates a Path by joining the strings with the separator of the default file system. Path.of("docs", "reports", "jan.txt") is docs/reports/jan.txt on Linux and macOS and docs\reports\jan.txt on Windows. A second overload, Path.of(URI), turns a file: URI back into a path.
It was added in Java 11 as the modern replacement for Paths.get, which does exactly the same thing and still works. Creating a path only parses text: the file does not need to exist.
A static factory method of the java.nio.file.Path interface, added in Java 11. Call it on the interface name, as in Path.of("docs", "a.txt"), after import java.nio.file.Path;. Requires Java 11 or newer. On Java 8, 9, or 10 use Paths.get(...) with import java.nio.file.Paths;; every other page on this site does so.
Requires Java 11 or newer
All three examples below call Path.of, so compile and run them with JDK 11 or later. Replace Path.of with Paths.get to run them on Java 8.
Syntax
Pass one string, several parts, or a URI:
Java
Path a = Path.of("docs/reports/jan.txt");
Path b = Path.of("docs", "reports", "jan.txt"); // equal to a
Path c = Path.of(URI.create("file:///home/anna/a.txt"));
Call
What it does
Path.of(String first)
Parse one path string.
Path.of(String first, String... more)
Join the parts with the separator; empty parts are skipped.
Path.of(URI uri)
Convert a URI, usually file:///..., into a path.
Java 11+. Paths.get(first, more...) and Paths.get(URI) simply call these methods in Java 11 and later.
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 — Create the Same Path in Three Ways
Build a report path from parts, from one string, and from a sloppy string with extra slashes, then compare the results. Java 11+.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathOfBasic {
public static void main(String[] args) {
Path a = Path.of("docs", "reports", "jan.txt");
Path b = Path.of("docs/reports/jan.txt");
Path c = Path.of("docs//reports/", "jan.txt");
System.out.println("a = " + a);
System.out.println("c = " + c);
System.out.println("a.equals(b): " + a.equals(b));
System.out.println("a.equals(c): " + a.equals(c));
System.out.println("same as Paths.get: " + a.equals(Paths.get("docs", "reports", "jan.txt")));
}
}
Output
Run with java PathOfBasic. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
a = docs/reports/jan.txt
c = docs/reports/jan.txt
a.equals(b): true
a.equals(c): true
same as Paths.get: true
How It Works
Parts are joined with the separator.Path.of("docs", "reports", "jan.txt") inserts the system separator between the three parts, so you never type a slash or backslash yourself.
Extra slashes disappear. The doubled and trailing slashes in c are merged while the text is parsed, which is why c prints exactly like a and the two are equal.
Path.of and Paths.get agree. In Java 11 and later Paths.get calls Path.of, so both produce equal paths; pick Paths.get only for Java 8 compatibility.
Example 2 — Turn a file: URI into a Path
Convert a file: URI with an encoded space into a path, then see what happens with an https URI and a URI without a scheme. Java 11+.
Run with java PathOfUri. Shown for Linux and macOS. On Windows the first line prints \home\anna\My Notes\todo.txt; the other lines are the same.
Path: /home/anna/My Notes/todo.txt
File: todo.txt
https URI -> FileSystemNotFoundException
no scheme -> IllegalArgumentException
How It Works
Percent codes are decoded. In a URI a space is written %20; Path.of(URI) decodes it, so the folder is named My Notes again.
The scheme picks the provider. file: is handled by the default file system; no provider is installed for https, so Java throws FileSystemNotFoundException instead of downloading anything.
A scheme is required. notes/todo.txt is a relative URI with no scheme, which Path.of(URI) rejects with IllegalArgumentException; use Path.of(String) for plain text.
Example 3 — Validate Path Text Before Using It
A helper reports whether text can become a path. The NUL character is invalid everywhere; empty parts are simply skipped. Java 11+.
Run with java PathOfInvalid. Shown for Linux and macOS. On Windows the last line prints docs\a.txt; Windows also rejects characters such as : and *, which Linux accepts.
Catch the unchecked exception.InvalidPathException extends IllegalArgumentException, so the compiler does not force a try block; the helper adds one because the text could come from a user.
Empty text is allowed.Path.of("") is the empty path, which has one empty name; it means "the current directory" when it is resolved later.
NUL is rejected, empty parts are not. The \0 character can never be part of a file name, while the empty strings in the last call are dropped before the parts are joined.
Frequently Asked Questions
None in behaviour. Path.of was added in Java 11 and is the recommended style there; Paths.get exists since Java 7 and calls Path.of in newer versions.
Java 11 or newer. On Java 8 use Paths.get with the same arguments.
No. It only builds a Path object from text. Use Files.createFile or Files.write to create the file.
The text contains a character the file system does not allow, such as the NUL character, or on Windows characters like : or * inside a name.
Call Path.of(uri), or Paths.get(uri) on Java 8. The URI must have a scheme, usually file, and must not contain a query or fragment.