Here's a simple use of the JNA library for creating hard links in Java:
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.io.File;
import java.io.IOException;
public final class HardLink {
private static final LibC LIBC = (LibC)Native.loadLibrary("c", LibC.class);
private HardLink() {
}
public static void link(File src, File dest) throws IOException {
if (LIBC.link(src.toString(), dest.toString()) != 0)
throw new IOException(LIBC.strerror(Native.getLastError()));
}
public static void main(String[] args) throws Exception {
System.out.println("Attempting to hardlink " + args[0] + " -> " + args[1]);
HardLink.link(new File(args[0]), new File(args[1]));
}
private interface LibC extends Library {
int link(String from, String to);
String strerror(int errno);
}
}
When this program is run on Mac OS X 10.7.4 (Lion) using JNA 3.4.0, it does work, but for some reason it is doing a fork()
- and I say that because it causes the Java icon to bouncy pop-up (appear) in the task bar with "HardLink" in the command bar as the program name.
My question is: why is JNA doing a fork()
(or is it)? Is there something wrong with this program?
Note: I'm not interested in other ways to hard link files from Java. This is just an example.
JNA loads some AWT classes unless you're running headless. It's the loading of AWT-related classes (including Swing) which cause the java process to appear in the OSX Dock.
Run your program with -Djava.awt.headless=true. This will prevent it from showing up in the OSX dock.