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.
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);
}
}
}
Output
Run with java CreateSymbolicLinkBasic. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
Is a symbolic link: true
Points to: data-v2.txt
Read through link: version 2
--- Windows, no symlink privilege ---
Symbolic links not allowed here: FileSystemException
How It Works
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.
Inspect, then read.isSymbolicLink is true, readSymbolicLink returns the stored text, and readAllBytes(link) follows the link to the content.
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.
Run with java CreateSymbolicLinkDangling. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
exists(link): false
exists(link, NOFOLLOW_LINKS): true
exists(link) after writing target: true
--- Windows, no symlink privilege ---
Symbolic links not allowed here: FileSystemException
How It Works
No check on creation. Java happily creates shortcut.txt even though later.txt does not exist; the link is "dangling".
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.
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);
}
}
}
Output
Run with java CreateSymbolicLinkFolder. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
current is a directory: true
app.jar via current: true
release-2 still a directory: true
app.jar still there: true
--- Windows, no symlink privilege ---
Symbolic links not allowed here: FileSystemException
How It Works
Folders can be targets. current points to release-2; on Windows, Java sees the target is a folder and makes a directory link.
Paths go through the link.current.resolve("app.jar") reaches the jar inside release-2, and isDirectory(current) follows the link.
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.