JDK-8003887 : File.getCanonicalFile() does not resolve symlinks on MS Windows
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 7u9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows
  • CPU: generic
  • Submitted: 2012-11-22
  • Updated: 2024-09-16
  • Resolved: 2024-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 24
24 b15Fixed
Related Reports
Relates :  
Description
Steps to reproduce:

Create a symlink on MS Windows (NTFS file system):
F:\> mkdir link-target
F:\> mklink /D link f:\link-target
symbolic link created for link <<===>> f:\link-target 

Run this Java code:
public static void main(String[] args) throws IOException {
    File file = new File("f:\\link");
    System.out.println(file);
    System.out.println(file.getCanonicalFile());
    System.out.println(file.getCanonicalFile().getCanonicalFile());
}

The result is:

f:\link
F:\link
F:\link

See also this NetBeans issue: http://netbeans.org/bugzilla/show_bug.cgi?id=222158
Comments
Changeset: 042053c3 Branch: master Author: Brian Burkhalter <bpb@openjdk.org> Date: 2024-09-05 15:03:54 +0000 URL: https://git.openjdk.org/jdk/commit/042053c3a82e9fbd4c6866efe872c1c92714e6e7
05-09-2024

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/20801 Date: 2024-08-30 20:59:18 +0000
30-08-2024

The JNI function canonicalize0() in WinNTFileSystem does not invoke the C function getFinalPath() on its result hence links are not resolved. It would be straightforward to implement link resolution when the file name, the last name in the name sequence, is a link, but otherwise more complicated processing would be needed. This would involve in effect replicating what is done in Path.toRealPath(), so it might be better simply to wait until such time as File is re-implemented on top of NIO.
15-08-2024

As a workaround, use toPath().toRealPath(boolean).
06-08-2013

Yes, it's on our list to enhance File.getCanonicalPath to support sym links, in the mean time you can use the new API, eg: file.toPath().toRealPath() and it will support sym links on Windows.
22-11-2012