我正在调用ProcessBuilder来执行Linux命令“ chkconfig”,如下所示:
ArrrayList<String> shellcomand = new ArrayList<>(Arrays.asList("chkconfig"));
String[] commandArr = shellCommand.toArray(new String[shellCommand.size()]);
ProcessBuilder pb = new ProcessBuilder(commandArr);
pb.redirectErrorStream(true);
Process p = pb.start();
但是,有时命令成功执行,有时会出现以下错误:
Caused by: java.io.IOException: error=2, No such file or directory
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)
为什么有时命令运行而有时却不运行?有趣的是,如果我用完整路径更改命令,即-“ / sbin / chkconfig”,一切正常。