java调用shell脚本

try {

String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
LOGGER.info("path:{}", path);
String shpath="/root/workspace/jar/test.sh";
String string = shpath + " " + "所带的参数";  //shell中$1,$2方式获取
Process ps = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c", string});
LOGGER.info("waitFor start");
ps.waitFor();
LOGGER.info("waitFor end");
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
LOGGER.info("br:{}", br);
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
LOGGER.info("line:{}", line);
sb.append(line).append("\n");
}
String result = sb.toString();
LOGGER.info("result:{}", result);
}
catch (Exception e) {
LOGGER.error("versionMgrHtml()", e);
e.printStackTrace();
}

posted @ 2019-04-12 13:02  jack_zou2018  阅读(625)  评论(0)    收藏  举报