Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a symbolic link with Java

Tags:

java

symlink

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.

like image 689
omnibrain Avatar asked Jul 29 '13 14:07

omnibrain


People also ask

What is a symbolic link file in Java?

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.

What is the command to create a symbolic link?

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.

Is it possible to create a symbolic link for a original file or directory that does not exist?

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.

What is a symbolic link example?

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.


1 Answers

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.

like image 93
omnibrain Avatar answered Oct 14 '22 01:10

omnibrain