1

I am trying to get the real path from a symbolic link but no success.

Operating system: Mac Java version: Java 9

I am seeing the documentation from: https://docs.oracle.com/javase/tutorial/essential/io/links.html

I have following folder structure on my computer...

 I have kept real file is in 
 /Users/Name/Desktop/realfile.txt

 I have created a symbolic link of realfile.txt as realfile_alias and kept it in
 /Users/Name/WorkSpace/Test/data/realfile_alias 

 I am running java program from
 /Users/Name/WorkSpace/Test/src folder.

My source code:

String sPath = "data/realfile_alias"  // this is relative path. I have kept the symbolic link in data folder. 

Path path = Paths.get(sPath);
boolean isSymbolic = Files.isSymbolicLink(path); 
// it returns true

Path realPath = Files.readSymbolicLink(path);
realPath is just returning 
/Users/Name/WorkSpace/Test/data/realfile_alias

But I expect it to be 

/Users/Name/Desktop/realfile.txt

How can I get the real path from symbolic path?

Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
  • Have you tried File.getCanonicalPath() ? see https://stackoverflow.com/a/2490387/7417402 – Devstr Feb 01 '18 at 23:31
  • how did you create the symbolic link? Also, could you please post the output of `ls -la /Users/Name/WorkSpace/Test/data/realfile_alias` ? – Devstr Feb 01 '18 at 23:34
  • output of ls -la: lrwxr-xr-x 1 sarker staff 12 Feb 1 18:41 realfile_alias -> realfile.txt – Md Kamruzzaman Sarker Feb 01 '18 at 23:42
  • thanks, it looks like a symbolic link indeed. Have you tried setting `sPath` to the absolute path i.e. `/Users/Name/WorkSpace/Test/data/realfile_alias` ? – Devstr Feb 01 '18 at 23:44

0 Answers0