In Java, Files.isSymbolicLink(path) returns true when the entry at path is itself a symbolic link. It never follows the link, so it works even when the target is missing (a dangling link).
Regular files, folders, hard links, and missing paths all return false. Use it before deleting or walking, to treat links differently from real files, and pair it with readSymbolicLink to see the target.
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.isSymbolicLink(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Pass the path; there are no options:
Java
if (Files.isSymbolicLink(p)) {
Path target = Files.readSymbolicLink(p);
}
Call
What it does
Files.isSymbolicLink(p)
Is p a link?
Files.readSymbolicLink(p)
Where does it point?
Files.exists(p)
Does the target exist (follows the link)?
Since Java 7. Creating links in the examples may need Developer Mode or admin rights on Windows, so each example falls back to a message when creation fails.
Example 1 — Test a File, a Folder, a Missing Path, and a Link
Only the actual link answers true.
Java
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
public class IsSymbolicLinkKinds {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.createFile(dir.resolve("real.txt"));
Path link = dir.resolve("alias.txt");
System.out.println("real.txt: " + Files.isSymbolicLink(file));
System.out.println("temp folder: " + Files.isSymbolicLink(dir));
System.out.println("missing: " + Files.isSymbolicLink(dir.resolve("nope")));
try {
Files.createSymbolicLink(link, file.getFileName());
System.out.println("alias.txt: " + Files.isSymbolicLink(link));
} catch (FileSystemException e) {
System.out.println("alias.txt: could not be created here");
} finally {
Files.deleteIfExists(link);
Files.delete(file);
Files.delete(dir);
}
}
}
Output
Run with java IsSymbolicLinkKinds. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
real.txt: false
temp folder: false
missing: false
alias.txt: true
--- Windows, no symlink privilege ---
real.txt: false
temp folder: false
missing: false
alias.txt: could not be created here
How It Works
Ordinary entries. A file and a folder are not links, so both print false.
No error for missing paths. nope does not exist, and the method just returns false.
The link itself. alias.txt points to real.txt and is reported as a link; if links cannot be created, the fallback line appears instead.
Example 2 — Spot a Dangling Link
A link is broken when isSymbolicLink is true but exists, which follows the link, is false.
Java
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class IsSymbolicLinkBroken {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path link = dir.resolve("old-logo.png");
try {
Files.createSymbolicLink(link, Paths.get("deleted-logo.png"));
boolean broken = Files.isSymbolicLink(link) && !Files.exists(link);
System.out.println("Is a link: " + Files.isSymbolicLink(link));
System.out.println("Target exists: " + Files.exists(link));
System.out.println("Broken: " + broken);
} catch (FileSystemException e) {
System.out.println("Could not create a link here");
} finally {
Files.deleteIfExists(link);
Files.delete(dir);
}
}
}
Output
Run with java IsSymbolicLinkBroken. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
Is a link: true
Target exists: false
Broken: true
--- Windows, no symlink privilege ---
Could not create a link here
How It Works
A link to nothing. deleted-logo.png does not exist, but Java creates the link anyway.
Two views.isSymbolicLink looks at the link entry (true), while exists follows it to the missing target (false).
The broken-link rule. Combining the two answers is how tools find dangling links to report or remove.
Example 3 — Show "name -> target" for Links
List a folder, sorted, and print where each link points.
Java
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class IsSymbolicLinkListing {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path v1 = Files.createFile(dir.resolve("app-1.0.jar"));
Path current = dir.resolve("app.jar");
try {
Files.createSymbolicLink(current, v1.getFileName());
List<Path> entries;
try (Stream<Path> s = Files.list(dir)) {
entries = s.sorted().collect(Collectors.toList());
}
for (Path p : entries) {
String line = p.getFileName().toString();
if (Files.isSymbolicLink(p)) {
line += " -> " + Files.readSymbolicLink(p);
}
System.out.println(line);
}
} catch (FileSystemException e) {
System.out.println("Could not create a link here");
} finally {
Files.deleteIfExists(current);
Files.delete(v1);
Files.delete(dir);
}
}
}
Output
Run with java IsSymbolicLinkListing. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
app-1.0.jar
app.jar -> app-1.0.jar
--- Windows, no symlink privilege ---
Could not create a link here
How It Works
Sorted entries. The listing is sorted so the output is stable; app-1.0.jar comes first because - sorts before the dot.
Only links get an arrow.isSymbolicLink picks out app.jar, and readSymbolicLink supplies the stored target.
Like ls -l. The result mirrors how Linux shows links, a common way to publish "the current version" of a file.
Frequently Asked Questions
Call Files.isSymbolicLink(path). It returns true only for the link entry itself and never follows the link.
Yes. It returns true for a link whose target is missing, which makes it the right tool for finding dangling links.
No. A hard link is just another name for a file, and isSymbolicLink returns false for it.
No. They are ordinary files that Windows Explorer interprets, so isSymbolicLink returns false.
Use Files.readSymbolicLink(link), which returns the stored target path.