问题描述
我正在寻找一个与标准 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 方法,但这些方法比简单地使用旧的 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:
- posix for Java(基于 JNI)[源自 Jython]
- jna-posix(基于 JNA)[源自 JRuby]
- posix for Java (based on JNI) [derived from Jython]
- jna-posix (based on JNA) [derived from JRuby]
我个人非常喜欢 JNA.看看我的这个例子,映射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 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!