// 读取shell脚本或者命令
public static void runShell(String shellString) {
try {
Process process = Runtime.getRuntime().exec(shellString);
int exitValue = process.waitFor();
if (0 != exitValue) {
log.error("call shell failed. error code is :" + exitValue);
} else {
getShellResult(process);
}
} catch (Throwable e) {
System.out.println(e);
log.error("call shell failed. " + e);
}
}
//收集shell脚本运行的结果集
public static List<String> getShellResult(Process process) {
List<String> processList = new ArrayList<String>();
try {
BufferedReader input = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
processList.add(line);
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}
for (String line : processList) {
System.out.println(line);
}
return processList;
}
public static void main(String[] args) {
runShell("/lw/shell/shell.txt");
}
java执行shell命令或者脚本,返回结果到程序
最新推荐文章于 2024-06-29 14:58:08 发布