Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java library of Unix functions?

I am looking for a Java library to interface with standard Unix functions, i.e. stat(), getpwuid(), readlink().

This used to exist, and was called javaunix. It was released back in 2000. See this announcement. But the project page is now gone.

Is there any modern replacement for these types of functions in Java today? One could make a system call to /bin/ls -l and parse the output, or write a custom JNI method, but these approaches are more work than simply using the old javaunix library.

Clarification -- In order to find out a file's owner, from a C program, it should call stat() which gives the UID of the owner, and then use getpwuid() to get the account's name from the UID. In Java this can be done through a custom JNI method, or the javaunix library which uses JNI.

like image 429
Kevin Panko Avatar asked Jul 06 '09 17:07

Kevin Panko


1 Answers

I'm aware of two compelling projects:

  • posix for Java (based on JNI) [derived from Jython]
  • jna-posix (based on JNA) [derived from JRuby]

Personally I like very much JNA. Take a look at this example of mine, mapping link(2):

import com.sun.jna.Library;
import com.sun.jna.Native;

class Link {

    private static final C c =
        (C) Native.loadLibrary("c", C.class);

    private static interface C extends Library {

        /** see man 2 link */
        public int link(String oldpath, String newpath);
    }

    @Override
    protected void hardLink(String from, String to) {
        c.link(to, from);
    }
}
like image 99
dfa Avatar answered Sep 19 '22 20:09

dfa