java执行shell命令或者脚本,返回结果到程序

来源:互联网 发布:Os软件 编辑:程序博客网 时间:2024/04/28 16:01
// 读取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");}

0 0
原创粉丝点击