我正在试验 Java 应用程序的 Linux 功能。我不想向解释器 (JVM) 添加功能,所以我尝试编写一个简单的包装器(将调试信息打印到标准输出):
#include <stdio.h>
#include <stdlib.h>
#include <sys/capability.h>
#include <unistd.h>
int main(int argc, char *argv[]){
cap_t cap = cap_get_proc();
if (!cap) {
perror("cap_get_proc");
exit(1);
}
printf("%s: running with caps %s\n", argv[0], cap_to_text(cap, NULL));
return execlp("/usr/bin/java", "-server", "-jar", "project.jar", (char *)NULL);
}
这样,我可以看到为这个可执行文件设置了能力:
./runner: running with caps = cap_net_bind_service+p
getcap
显示
runner = cap_net_bind_service+ip
我设置的能力是可以继承的,所以应该没有问题。但是,java 仍然不想绑定(bind)到特权端口。
我收到这个错误:
sun/nio/ch/Net.java:-2:in `bind': java.net.SocketException: Permission denied (NativeException)
有人可以帮我解决这个问题吗?
最佳答案
尝试使用 1024 以上的端口,或以 root 身份运行。
关于java - 功能与 Linux 和 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3036857/