Java: check symbolic link file existence
Asked Answered
W

5

12

We talk about java 1.6 here. Since symoblic link is not yet supported, how can examine the existence of them.

1: tell wheather the link file itself exists (return true even if the link is broken)

2: follow the link and tell wheather underlying file exists.

Is there really no way to realize this except JNI?

Winou answered 1/2, 2010 at 9:31 Comment(1)
A better solution copied from Apache FileUtils can be found here: stackoverflow.com/questions/813710/…Sidwohl
G
5

looks that way for now... unless you go with openjdk http://openjdk.java.net/projects/nio/javadoc/java/nio/file/attribute/BasicFileAttributes.html#isSymbolicLink()

Gastrin answered 1/2, 2010 at 9:48 Comment(2)
Just in case anyone would wonder, the static method you could use is java.nio.file.Files.isSymbolicLink(Path path).Venusberg
Now link is returning 404, this why you should take extra care before adding any link to your answer.Evocator
R
5

It is slow but you could compare getAbsolutePath() and getCanonicalPath() of a File. So use only outside a performance critical code path.

Reconvert answered 2/8, 2010 at 9:39 Comment(1)
This doesn't work for Windows links, only *nix symbolic links.Flem
D
1

The following should give you a start:

if (file.exists() && !file.isDirectory() && !file.isFile()) {
    // it is a symbolic link (or a named pipe/socket, or maybe some other things)
}

if (file.exists()) {
    try {
        file.getCanonicalFile();
    } catch (FileNotFoundException ex) {
        // it is a broken symbolic link
    }
}

EDIT : The above don't work as I thought because file.isDirectory() and file.isFile() resolve a symbolic link. (We live and learn!)

If you want an accurate determination, you will need to use JNI to make the relevant native OS-specific library calls.

Deuteragonist answered 1/2, 2010 at 9:50 Comment(4)
Thanks, but file.exists() will return false for symbolic-link I think.Winou
It doesn't on Linux. But unfortunately, file.isFile() and file.isDirectory() resolve a symbolic link and return a result based on the type of the resolved object.Deuteragonist
It return false on Linux. You can also refer the evaluation part of bugs.sun.com/bugdatabase/view_bug.do?bug_id=4956115.Winou
@Winou - it returned true on Linux when I tested it for a symbolic link to an existing file. The point is that it tests both whether the link AND the target of the link exists.Deuteragonist
S
1

hmm..not yet supported..will it ever be supported is the question that comes to my mind looking at this question...symbolic links are platform specific and Java is supposed to b platform independent. i doubt if anything of the sort is possible with java as such..you may have to resort to native code for this portion and use JNI to bridge it with the rest of your program in java.

Supranational answered 1/2, 2010 at 12:59 Comment(2)
Java1.7 is going to implement symbolic-link interface, so what's their consideration for platform independent?Winou
hmm..thats interesting..you may want to take a look at this : stackoverflow.com/questions/813710/…Supranational
G
1

#2 is easy: just use File.isFile etc., which will traverse the link. The hard part is #1.

So if you cannot use java.nio.file, and want to avoid JNI etc., the only thing I found which works:

static boolean exists(File link) {
    File[] kids = link.getParentFile().listFiles();
    return kids != null && Arrays.asList(kids).contains(link);
}

Not terribly efficient but straightforward.

Gadolinium answered 1/5, 2013 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.