Given a Java 'File' object, how can I detect whether or not it refers to a symlink? Then resolve its real absolute path.
File.getCanonicalPath() resolves symlinks:
From JDK8+, you can leverage Files.isSymbolicLink(Path path) to check the give path is symbolic or not.
The technique used in Apache Commons uses the canonical path to the parent directory, not the file itself. I don't think that you can guarantee that a mismatch is due to a symbolic link, but it's a good indication that the file needs special treatment.
This is Apache code (subject to their license), modified for compactness:
Then below is the testing groovy code:
* Using PosixFileAttributes : File Attribute