baskin
baskin

Reputation: 1653

Creating a Hard Link in java

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

Answers (4)

Benedikt Waldvogel
Benedikt Waldvogel

Reputation: 12866

It’s easy in Java 7 using createLink:

Files.createLink(Paths.get("newlink"), Paths.get("existing"));

Upvotes: 16

bajafresh4life
bajafresh4life

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

Tom Hawtin - tackline
Tom Hawtin - tackline

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

dfa
dfa

Reputation: 116342

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

Upvotes: 5

Related Questions