views:

772

answers:

4

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?

+4  A: 

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

Ryan Fernandes
A: 
Stephen C
Thanks, but file.exists() will return false for symbolic-link I think.
solotim
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.
Stephen C
It return false on Linux. You can also refer the evaluation part of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4956115.
solotim
@solotim - 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.
Stephen C
Yes, Stephen, you are right, I'm wrong. :P
solotim
+1  A: 

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.

Aadith
Java1.7 is going to implement symbolic-link interface, so what's their consideration for platform independent?
solotim
hmm..thats interesting..you may want to take a look at this : http://stackoverflow.com/questions/813710/java-1-6-determine-symbolic-links
Aadith
+1  A: 

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