>在Java中,如何执行带有选项的linux程序,如下所示:
ls -a(选项为-a),
另一个:./ myscript name = john age = 24
我知道如何执行命令,但是无法执行该选项.
解决方法:
用示例更新
我从清单示例中直接摘下来并进行了修改,以便可以在PC上进行测试,并且运行正常
private static void copy(InputStream in, OutputStream out) throws IOException {
while (true) {
int c = in.read();
if (c == -1) {
break;
}
out.write((char) c);
}
}
public static void main(String[] args) throws IOException, InterruptedException {
// if (args.length == 0) {
// System.out.println("You must supply at least one argument.");
// return;
// }
args = new String[] {"cmd", "/c", "dir", "C:\\"};
ProcessBuilder processBuilder = new ProcessBuilder(args);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
copy(process.getInputStream(), System.out);
process.waitFor();
System.out.println("Exit Status : " + process.exitValue());
}
标签:java,terminal,command
来源: https://codeday.me/bug/20191013/1908161.html