In linux we can create relative symlinks.
ln -s targetfile symlink
In java NIO.2
Files.createSymbolicLink(symlink, targetfile);
this code not working with relative paths.
Below is the code to give relative path while creating Symbolic links in order to make it accessible in cross platform environments.
Path source = Paths.get("D:\\A\\B\\C\\D\\E\\F\\G\\a.mp4"); // original file's absolute path
Path link = Paths.get("D:\\A\\B\\C\\D\\E\\F\\H\\I\\a.mp4"); // symbolic link's absolute path
Path relativeSrc = link.getParent().relativize(source); // relative path of original file from symbolic link
link.getParent().toFile().mkdirs(); // create the directory hierarchy if any folder is not available
Files.createSymbolicLink(link, reativeSrc); // create symbolic link.
Hope this will help :)
Files.createSymbolicLink(destination, targetDirectory.relativize(targetfile));
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