• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Most Efficient way to create SymLinks in Java

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,

Is there a way we can create Symlinks in Java ? either using an API or an already existing open source or 'recommended' way of accomplishing this.

Thanks a bunch
_SR
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since symlinks are platform specific (Windows doesn't support them, for one) it is not built in the API. You'll need to find either a third party library that has done the work for you, or use JNI yourself.

The native calls you will be looking for are readlink for resolving links, symlink for creating them, lstat for querying them and unlink for removing them.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since symlinks are platform specific (Windows doesn't support them, for one)
Not true in Vista: Using Symlinks in Windows Vista. Admittedly an overdue addition.

You could consider just executing an ln -s (or mklink!) command too, via Runtime's exec methods. Not as pleasant as a built-in API, but is quick and easy to use and should work.
[ June 26, 2008: Message edited by: Charles Lyons ]
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use java.lang.ProcessBuilder or java.lang.Runtime to execute the command-line equivalents of the commands Rob mentions (ln, rm and so on). This strategy comes with some baggage, however.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Lyons:
Not true in Vista: Using Symlinks in Windows Vista. Admittedly an overdue addition.

Actually, that functionality was available in previous versions as well - at least in XP and possibly also in 2000. It wasn't available through the default tools in Windows though, but several 3rd party applications could get the job done.

The thing is, those aren't the real symlinks you know from Linux and Unix. Those can point to anything; in Windows you can only create soft links to folders (not files), and hard links to files (not folders). There is no such limitation in Linux and Unix.
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that it helps you now, but Java 7 should include java.nio.filesystems.PathReference.createLinkTo(target) via JSR 203.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic