Creating a Hard Link in java

后端 未结 5 1090
栀梦
栀梦 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: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!

提交回复
热议问题