Reputation: 1653
Currently I use 'ln
' command via Runtime.exec()
. It works fine. The only problem is that in order to do this fork, we need twice the heap space of the application. My app is a 64 bit app with heap size around 10Gigs and thus its runs out of swap space. I couldn't find any configuration that could fix this.
I also want not to use JNI for the same. Also I had heard somewhere that this facility will soon be provided in java 7.
Upvotes: 14
Views: 8313
Reputation: 12866
It’s easy in Java 7 using createLink:
Files.createLink(Paths.get("newlink"), Paths.get("existing"));
Upvotes: 16
Reputation: 12853
This is very easy with JNA:
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
int link(String fromFile, String toFile);
}
public static void main(String[] args) {
CLibrary.INSTANCE.link(args[0], args[1]);
}
Compile and run!
Upvotes: 2
Reputation: 147164
You could use Windows instead of UNIX? ;) I believe JDK7 will be using a call similar to CreateProcess instead of fork where available.
A more practical solution would be to create a new child process soon after starting. If you are using a 10g heap, another small Java process probably wont be so bad. Get that process (via use of streams) to exec.
Upvotes: -3