且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Java 执行系统命令 (linux/bsd)

更新时间:2023-12-03 23:32:46

你的方式与我可能会做的差不多:

Your way isn't far off from what I'd probably do:

Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";

while ((line = b.readLine()) != null) {
  System.out.println(line);
}

b.close();

当然可以处理您关心的任何异常.

Handle whichever exceptions you care to, of course.