Skip to content

Commit

Permalink
Get file absolute path from java.nio.Path.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Dec 1, 2024
1 parent 21b97a1 commit 4a576c6
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ public static String getFileAbsolutePath(String f) {
return getFileAbsolutePath(new File(f));
}

public static String getFileAbsolutePath(Path path) {
try {
if (!PlatformUtils.isWindowsPlatform()) {
// We don't want to follow links on Linux.
return path.toRealPath(LinkOption.NOFOLLOW_LINKS).toString();
} else {
// On Windows, this is needed to get the proper case of files (because it's case-preserving
// and we have to get the proper case when resolving module names).
// Especially annoying if something starts with 'C:' and sometimes is entered with 'c:'.
// Note: this doesn't resolve `substs` on Windows (which is good).
return path.toFile().getCanonicalPath();
}
} catch (IOException e) {
return path.toFile().getAbsolutePath();
}
}

/**
* This version does not resolve links on Linux.
*/
Expand All @@ -244,6 +261,7 @@ public static String getFileAbsolutePathNotFollowingLinks(File f) {
// On Windows, this is needed to get the proper case of files (because it's case-preserving
// and we have to get the proper case when resolving module names).
// Especially annoying if something starts with 'C:' and sometimes is entered with 'c:'.
// Note: this doesn't resolve `substs` on Windows (which is good).
return f.getCanonicalPath();
}
} catch (IOException e) {
Expand Down

0 comments on commit 4a576c6

Please sign in to comment.