Creating a Hard Link in java

后端 未结 5 1046
栀梦
栀梦 2021-01-11 12:51

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 o

相关标签:
5条回答
  • 2021-01-11 13:06

    The answer can be read in Oracle The Java™ Tutorials Links, Symbolic or Otherwise

    0 讨论(0)
  • 2021-01-11 13:17

    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.

    0 讨论(0)
  • 2021-01-11 13:21

    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!

    0 讨论(0)
  • 2021-01-11 13:22

    It’s easy in Java 7 using createLink:

    Files.createLink(Paths.get("newlink"), Paths.get("existing"));
    
    0 讨论(0)
  • 2021-01-11 13:31

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

    0 讨论(0)
提交回复
热议问题