I'm having trouble creating a symbolic link to a directory in Java. I'm using the createSymbolicLink() method from the Files class: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
Absolute paths:
- Target:
/some/path/target
- Link:
/some/path/xxx/linkname
I would expect that a link in the directory /some/path/xxx/
is created to the folder /some/path/target
, but instead a link from /some/path/xxx/linkname
to /some/path/xxx/target
is created. I just can't figure out what I'm doing wrong.
When I create a link from /some/path/linkname
to /some/path/target
, everything works as expected.
Any help is greatly appreciated.
EDIT: Here's my code:
Path records = Paths.get(Properties.getProperty("records.path"));
Path recordsLink = Paths.get(Properties.getProperty("webserver.root") + System.getProperty("file.separator") + records.getFileName());
try {
Files.createSymbolicLink(recordsLink, records);
} catch (IOException e) {
e.printStackTrace();
}
The "records.path" and "webserver.root" are both relative paths.
Actually I just found a solution to the problem: It works if I do this:
records = records.toAbsolutePath();
I assumed the createSymbolicLink()
will use absolute paths to create the links, which was wrong.
cwd
(current working directory).. Which under java can sometimes be anywhere. If one wanted to use a relative path; you need tocd
to the folder where you want the link. See: Java Change File Working Directory – Tyeshatyg