tags:

views:

707

answers:

3

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.

A: 

I don't know any library with the Unix functions.

for most of the functions, I believe, you can use the standard Java API to do what you want. for example, there's no need to use the command ls to read the files of some directory. but in some specific cases, like stat (to find out if a file is a link) you have to use JNI.

cd1
A: 

I would be surprised to see one, considering it would almost necessarily be platform-specific. Java is not the best tool for that job. But you could certainly hook in via JNI or calls out to external programs if you insist. Or perhaps look into Groovy, which I understand is reasonably good for shell scripting, though I have no personal experience with it.

Rob H
+7  A: 

I'm aware of two compelling projects:

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);
    }
}
dfa
That looks good! I had not heard about JNA before! Seems to be located at https://jna.dev.java.net/
Kevin Panko