/**
* 2.Java代码调用shell脚本
*/
public static int exec(String cmd) throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream(), "utf-8"));
String inline;
while ((inline = br.readLine()) != null) {
logger.info(inline);
}
br.close();
BufferedReader error = new BufferedReader(new InputStreamReader(p
.getErrorStream(), "utf-8"));
String err;
while ((err = error.readLine()) != null) {
logger.info(err);
}
error.close();
int result = p.waitFor();
logger.info(cmd + " finished. result=" + result);
return result;
}
Java常用代码实现——执行脚本
最新推荐文章于 2024-06-26 17:32:52 发布