java跑cmd

java 跑cmd,直接使用:

String cmd = "sh xxx.sh param1 param2";   
Process process = Runtime.getRuntime().exec(cmd);

// 需要把输出拿一下,不然一闪就过去了。
try (InputStream inputStream = process.getInputStream()) {
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  String line = null;
  while ((line = bufferedReader.readLine()) != null) {
     //System.out.println(line);
 }
}

// 加一个 waitFor 等待cmd 返回.
if (process.waitFor() != 0) {
  System.out.println("process 已经结束:" + guid);
}
posted @ 2021-12-28 16:56  张鲸落  阅读(34)  评论(0编辑  收藏  举报