Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Most Efficient way to create SymLinks in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Most Efficient way to create SymLinks in Java" Watch "Most Efficient way to create SymLinks in Java" New topic
Author

Most Efficient way to create SymLinks in Java

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72
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

Joined: Oct 27, 2005
Posts: 19726
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

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

Joined: May 27, 2008
Posts: 225

Not that it helps you now, but Java 7 should include java.nio.filesystems.PathReference.createLinkTo(target) via JSR 203.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Most Efficient way to create SymLinks in Java