Java Path of() Method

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

Definition

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"));
CallWhat 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.

Library declarations
Java
// java.nio.file.Path (interface), Java 11+
static Path of(String first, String... more)
static Path of(URI uri)

Parameters

ParameterDescription
firstThe path string, or its first part.
moreOptional further parts, joined with the separator; empty strings are ignored.
uriAn absolute URI with a scheme, such as file:///home/anna/a.txt.

Return Value

Return ValueDescription
PathA new path of the default file system (or of the provider that matches the URI scheme).

Compare with Similar Methods

Other ways to get a path:

MethodHow it differs
Paths.get(...)Same result, available since Java 7; use it for Java 8 code.
resolve()Builds a new path from an existing one plus a child name.
toUri()The opposite direction of Path.of(URI).

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 — 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")));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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+.

Java
import java.net.URI;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Path;

public class PathOfUri {
    public static void main(String[] args) {
        Path todo = Path.of(URI.create("file:///home/anna/My%20Notes/todo.txt"));
        System.out.println("Path: " + todo);
        System.out.println("File: " + todo.getFileName());
        try {
            Path.of(URI.create("https://example.com/todo.txt"));
        } catch (FileSystemNotFoundException e) {
            System.out.println("https URI -> " + e.getClass().getSimpleName());
        }
        try {
            Path.of(URI.create("notes/todo.txt"));
        } catch (IllegalArgumentException e) {
            System.out.println("no scheme -> " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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+.

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

public class PathOfInvalid {
    static void check(String label, String text) {
        try {
            Path p = Path.of(text);
            System.out.println(label + ": valid, " + p.getNameCount() + " name(s)");
        } catch (InvalidPathException e) {
            System.out.println(label + ": " + e.getClass().getSimpleName());
        }
    }

    public static void main(String[] args) {
        check("notes.txt", "notes.txt");
        check("empty string", "");
        check("NUL character", "bad\0name.txt");
        System.out.println("empty parts skipped: " + Path.of("", "docs", "", "a.txt"));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: Path.relativize()

Build the relative path that leads from one path to another.

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

10 people found this page helpful