问题描述
我正在寻找一个与标准Unix函数接口的Java库,即 stat()
, getpwuid()
, readlink()
。
I am looking for a Java library to interface with standard Unix functions, i.e. stat()
, getpwuid()
, readlink()
.
这曾经存在,被称为 javaunix
。它于2000年发布。请参阅此。但是项目页面现在已经消失。
This used to exist, and was called javaunix
. It was released back in 2000. See this announcement. But the project page is now gone.
今天Java中是否有这些类型的函数的现代替代品?可以对 / bin / ls -l 进行系统调用并解析输出,或编写自定义JNI方法,但这些方法比简单使用旧方法更有效。 code> javaunix
库。
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.
澄清 - 为了找到文件的所有者,来自一个C程序,它应该调用 stat()
,它给出了所有者的UID,然后使用 getpwuid()
从UID获取帐户的名称。在Java中,这可以通过自定义JNI方法或使用JNI的 javaunix
库来完成。
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.
推荐答案
我知道两个引人注目的项目:
I'm aware of two compelling projects:
- (基于JNI)[派生自Jython]
- (基于JNA)[派生自JRuby]
- posix for Java (based on JNI) [derived from Jython]
- jna-posix (based on JNA) [derived from JRuby]
我个人非常喜欢。看看我的这个例子,映射 link(2)
:
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);
}
}
这篇关于是否有Unix函数的Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!