Unix上的sudo命令有效

时间:2018-09-20 14:07:30

标签: java unix sudo processbuilder

我正在unix sudo命令下运行,并且在那里运行正常。
     sudo -u xfbcft -i
问题是,当我尝试使用相同的Java进程生成器原因时,我在Unix上遇到权限被拒绝的异常。

public class Test {
public static void main(String args[]){
    System.out.println("Going to execute:");
    runUnixComand();
}

private static void runUnixComand() {
    Process p;
    StringBuffer output = new StringBuffer();

    try {
        String [] cmdArray ={"sudo -u xfbcft -i ","cftping -v","exit"};

        ProcessBuilder builder = new ProcessBuilder(cmdArray);
        builder.redirectErrorStream(true);
        //builder.redirectOutput();
        Process process = builder.start();
        InputStream is = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println("op:"+line);
        }
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
}}

下面是我在Unix终端上看到的异常行。

java.io.IOException: Cannot run program "sudo -u xfbcft -i ": error=13, Permission denied
java.io.IOException: Cannot run program "sudo -u xfbcft -i ": error=13, Permission denied
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at JdbcTest.runUnixComand(JdbcTest.java:23)
        at JdbcTest.main(JdbcTest.java:8)
Caused by: java.io.IOException: error=13, Permission denied
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
        at java.lang.ProcessImpl.start(ProcessImpl.java:134)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)

注意:我在sudoers文件中有条目,并且单个命令在Unix上运行良好。另外,运行此Java程序的用户和单个命令是相同的。请指导。

1 个答案:

答案 0 :(得分:0)

我如下更改了我的代码,它的工作原理就像魅力。

ExceptionInstrumentationUtil.java:212

所以要从bin / sh执行它