14

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
baskin
  • 1,643
  • 1
  • 18
  • 29

4 Answers4

15

It’s easy in Java 7 using createLink:

Files.createLink(Paths.get("newlink"), Paths.get("existing"));
Benedikt Waldvogel
  • 12,406
  • 8
  • 49
  • 61
5

you could try JNA in place of JNI (JNA has some clear advantages over JNI); yes, check the JSR 203

dfa
  • 114,442
  • 31
  • 189
  • 228
2

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!

bajafresh4life
  • 12,491
  • 5
  • 37
  • 46
  • It compiles, but it does not run. Not under Windows, that is. I haven’d yet groked the magic that JNA performs, but it seems the name of the respective function in MSVCRT is very different. – Michael Piefel Mar 04 '11 at 08:02
  • According to http://stackoverflow.com/questions/6015006/whats-the-name-of-the-create-hard-link-function-in-msvcrt, the function is called `CreateHardLink` and it's located in Kernel32, not in MSVCRT (which makes sense). – Aaron Digulla May 16 '11 at 09:11
-3

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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305