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:
/some/path/target
/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.
Soft/Symbolic link is a file pointer that behaves as the file that is linking to – if the target file gets deleted then the link is unusable. A hard link is a file pointer that mirrors the file that it's linking to, so it's basically like a clone. If the target file gets deleted, the link file is still valid.
Ln Command to Create Symbolic Links Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists. Source is the file or directory being linked to.
Additionally, you can create a symbolic link across mounted file systems, which you cannot do with a hard link. A symbolic link is another file that contains the path name for the original file—in essence, a reference to the file. A symbolic link can refer to a path name for a file that does not exist.
A symbolic link creates a file in your directory and acts as a shortcut to a file or folder. For example: I have a directory- let's say example.com. However, I want a shortcut to another directory within the example.com. To do this, you would create a symbolic link.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With