简体   繁体   中英

Creating a symbolic link with Java

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.

I found the solution to the problem: It works if I do this:

records = records.toAbsolutePath();

I assumed createSymbolicLink() will use absolute paths to create the links, which was wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM