I’m using Oracle Java 7 on Windows 64 bit.
When I create a symlink using Files.createSymbolicLink, I notice this behavior:
- If the target is a directory, a “directory symlink” is created.
- If the target is a file, a “file symlink” is created.
- If the target does not exist, a “file symlink” is created.
The type of the symlink is fixed and never changes, regardless of any changes to its target.
Using Windows’ native mklink command, it is possible to force the link type to be a “directory symlink”. Is it possible to achieve this using the native Java API or some library?
One trivial and ugly way is:
- If the target is a directory, just create the link
- If the target doesn’t exist, create a new empty target directory, create the link, and delete the directory.
- If the target is a file … handle it (move it, apply #2, move it back).
Fugly.
Unfortunately I don’t see a way within the Java APIs to do it.
I checked the Windows JRE code and it looks like the decision is based on the file attributes themselves:
The attributes themselves originate from native code, and it looks like there is no way to affect them.
Clearly you have other options like manually invoking
mklinkor even manipulating the returned objects using something likePowerMock(which is clearly not meant for this purpose).Another dirty option is to create proxys of all the relevant classes:
Path,FileSystemandFileSystemProvider.The way that it works is that the
Pathreturns aFileSystemwhich returns aFileSystemProvider– what you need to do is modify how theFileSystemProvider.createSymbolicLinkmethods behaves.The
createSymbolicLinkmethod receives a varargs argument which is currently not used – you can pass an argument to it that will indicate your wrapper that it needs to override the way symbolic links are created – and there you go 🙂After writing all of this – the only question I have is – why do you need this kind of behavior?