我在使用 r.exec 调用一些简单的命令行函数时遇到问题 - 出于某种原因,给定文件 X,命令 'echo full/path/to/X' 工作正常(在显示和使用 'p.exitValue ()==0',但 'cat full/path/to/X' 没有(并且有 'p.exitValue()==1') - 'cat' 和 'echo' 都存在于我的 /bin/ 中OSX - 我错过了什么吗?代码如下(碰巧,欢迎任何改进代码的建议......)
private String takeCommand(Runtime r, String command) throws IOException {
String returnValue;
System.out.println("We are given the command" + command);
Process p = r.exec(command.split(" "));
InputStream in = p.getInputStream();
BufferedInputStream buf = new BufferedInputStream(in);
InputStreamReader inread = new InputStreamReader(buf);
BufferedReader bufferedreader = new BufferedReader(inread);
// Read the ls output
String line;
returnValue = "";
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
returnValue = returnValue + line;
}
try {// Check for failure
if (p.waitFor() != 0) {
System.out.println("XXXXexit value = " + p.exitValue());
}
} catch (InterruptedException e) {
System.err.println(e);
} finally {
// Close the InputStream
bufferedreader.close();
inread.close();
buf.close();
in.close();
}
try {// should slow this down a little
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
return returnValue;
}