Create Windows Symbolic Link with Java (equivalent to MKLINK)
Asked Answered
A

2

8

Could anyone please tell me how to make a symbolic link (in the same way MKLINK does) and/or remove a symbolic link with Java. I have found solutions that use Java as a wrapper and use a Windows native program to accomplish this, but I really want a pure Java solution. Thank you in advance!

Americanist answered 7/10, 2013 at 16:59 Comment(0)
L
12

Since Java 7 you can do this easily using the NIO package.

Path target = Paths.get("target");
Path link = Paths.get("link");
Files.createDirectory(target);
Files.createSymbolicLink(link, target);

Do remember that you do need the correct privileges for this. In my unit test I had to run eclipse as an administrator to make it work (same as that I couldn't create a link from a normal cmd.exe)

Loquacity answered 11/3, 2015 at 9:33 Comment(1)
Alternatively, you can enable Developer Mode (on Windows 10 version 1703 or newer) which will allow you to create symlinks without without running as admin.Mercie
S
-3

As far as I know window does not have real symbolic links like Unix-like system do.

However Windows has the following relevant tools:

  1. You can map network drive, i.e. attach drive letter to specified network path. You can definitely do this using WMI. To access WMI from java take a look on tools like JaWin, Jinterop, Jintegra or write WMI script in JScript o VBScript and execute is from Java.

  2. You can use command subst that assigns letter to local file system path. This is the closest approach to Unix soft link.

  3. You can create desktop shortcut. Create one manually and take a look on it. Shortcut is actually regular text file (as far as I remember in INI format). You can easily create one using any language you want including java. This is not soft link but it is clickable.

Schnurr answered 7/10, 2013 at 17:16 Comment(1)
Well Windows Vista/7 has some sort of symbolic links. The MKLINK command-line utility accomplishes this. Is there no way in Java to emulate what this utility does?Americanist

© 2022 - 2024 — McMap. All rights reserved.