Java Files createSymbolicLink() Method

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

Definition

In Java, Files.createSymbolicLink(link, target) creates a symbolic link (symlink): a small entry named link that stores the path target. Opening the link opens whatever the target path names.

The target may be relative (resolved from the link's own folder), may be a folder or on another drive, and need not exist yet. readSymbolicLink reads the stored path back.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.createSymbolicLink(...), after import java.nio.file.Files;. You never create a Files object.

Windows needs extra rights.

On Windows you need administrator rights or Developer Mode (recent JDKs use it). Otherwise the call throws FileSystemException; the examples catch it.

Syntax

New link first, target second:

Java
Files.createSymbolicLink(link, Paths.get("data-v2.txt"));  // relative target
Path stored = Files.readSymbolicLink(link);                // data-v2.txt
CallWhat it does
createSymbolicLink(link, target)Creates link pointing to target.
Files.isSymbolicLink(p)Checks whether p itself is a link.

Since Java 7. Most Files methods follow links; pass LinkOption.NOFOLLOW_LINKS to look at the link itself.

Library declarations
Java
public static Path createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException

Parameters

ParameterDescription
linkThe link to create; must not exist.
targetThe path to store. Relative targets are resolved against the link's parent folder when used.
attrsOptional attributes for the link itself; rarely used.

Return Value

Return ValueDescription
PathThe link path.

Compare with Similar Methods

Links compared:

MethodHow it differs
createLink()Hard link: same data, same drive, no privilege needed on Windows.
readSymbolicLink()Reads the target path stored in a link.
isSymbolicLink()Tests whether a path is a symbolic link.

Java Examples

Example 1 — A "latest" Link to a Versioned File

Create latest.txt pointing to data-v2.txt with a relative target, then read through the link.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateSymbolicLinkBasic {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path target = dir.resolve("data-v2.txt");
        Files.write(target, "version 2".getBytes(StandardCharsets.UTF_8));
        Path link = dir.resolve("latest.txt");
        try {
            Files.createSymbolicLink(link, Paths.get("data-v2.txt"));
            System.out.println("Is a symbolic link: " + Files.isSymbolicLink(link));
            System.out.println("Points to: " + Files.readSymbolicLink(link));
            System.out.println("Read through link: " + new String(Files.readAllBytes(link), StandardCharsets.UTF_8));
        } catch (FileSystemException e) {
            System.out.println("Symbolic links not allowed here: " + e.getClass().getSimpleName());
        } finally {
            Files.deleteIfExists(link);
            Files.delete(target);
            Files.delete(dir);
        }
    }
}

How It Works

  1. Relative target. Paths.get("data-v2.txt") is stored as-is and resolved from the link's own folder, so the pair still works if the whole folder moves.
  2. Inspect, then read. isSymbolicLink is true, readSymbolicLink returns the stored text, and readAllBytes(link) follows the link to the content.
  3. Privilege fallback. Without the Windows privilege the creation throws FileSystemException; finally cleans up either way.

Example 2 — A Dangling Link Comes to Life

Create a link to a file that does not exist, check it with and without following links, then create the target.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;

public class CreateSymbolicLinkDangling {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path target = dir.resolve("later.txt");
        Path link = dir.resolve("shortcut.txt");
        try {
            Files.createSymbolicLink(link, target.getFileName());
            System.out.println("exists(link): " + Files.exists(link));
            System.out.println("exists(link, NOFOLLOW_LINKS): " + Files.exists(link, LinkOption.NOFOLLOW_LINKS));
            Files.write(target, "now here".getBytes(StandardCharsets.UTF_8));
            System.out.println("exists(link) after writing target: " + Files.exists(link));
        } catch (FileSystemException e) {
            System.out.println("Symbolic links not allowed here: " + e.getClass().getSimpleName());
        } finally {
            Files.deleteIfExists(link);
            Files.deleteIfExists(target);
            Files.delete(dir);
        }
    }
}

How It Works

  1. No check on creation. Java happily creates shortcut.txt even though later.txt does not exist; the link is "dangling".
  2. Following versus not following. Plain exists follows the link to a missing target and says false; with NOFOLLOW_LINKS it looks at the link entry itself and says true.
  3. Fixed by the target. Writing later.txt makes the same link usable, and exists(link) turns true.

Example 3 — A "current" Link to a Release Folder

Link to a folder, reach a file through the link, then delete the link and show the folder is untouched.

Java
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CreateSymbolicLinkFolder {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path release = Files.createDirectory(dir.resolve("release-2"));
        Path app = Files.createFile(release.resolve("app.jar"));
        Path current = dir.resolve("current");
        try {
            Files.createSymbolicLink(current, release.getFileName());
            System.out.println("current is a directory: " + Files.isDirectory(current));
            System.out.println("app.jar via current: " + Files.exists(current.resolve("app.jar")));
            Files.delete(current);
            System.out.println("release-2 still a directory: " + Files.isDirectory(release));
            System.out.println("app.jar still there: " + Files.exists(app));
        } catch (FileSystemException e) {
            System.out.println("Symbolic links not allowed here: " + e.getClass().getSimpleName());
        } finally {
            Files.deleteIfExists(current);
            Files.delete(app);
            Files.delete(release);
            Files.delete(dir);
        }
    }
}

How It Works

  1. Folders can be targets. current points to release-2; on Windows, Java sees the target is a folder and makes a directory link.
  2. Paths go through the link. current.resolve("app.jar") reaches the jar inside release-2, and isDirectory(current) follows the link.
  3. Deleting the link is safe. Files.delete(current) removes only the link entry; release-2 and app.jar stay, which is how deployment tools switch versions.

Frequently Asked Questions

It needs the symbolic-link privilege, which administrators have; recent Windows and JDK versions also allow it in Developer Mode. Otherwise Java throws FileSystemException ("A required privilege is not held by the client").
Yes; such a link is called dangling. exists(link) returns false until the target appears, while exists(link, LinkOption.NOFOLLOW_LINKS) returns true.
No. It is resolved against the folder that contains the link.
No. Files.delete(link) removes only the link. The target file or folder stays where it is.
Call Files.readSymbolicLink(link). It returns the stored target path exactly as it was given, which may be relative.

Next: Files.createTempDirectory()

Create a new empty folder with a unique random name: a safe scratch space for any task.

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

12 people found this page helpful