java执行linux脚本 并返回其执行结果
生活随笔
收集整理的這篇文章主要介紹了
java执行linux脚本 并返回其执行结果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;/*** 執行腳本的工具類*/
public class ExecutionScriptUtils {public static List<String> executeLinuxCmd(String cmd) throws IOException {System.out.println("got cmd job : " + cmd);Runtime run = Runtime.getRuntime();
// Process process = run.exec(cmd);Process process = run.exec(new String[] {"/bin/sh", "-c", cmd});InputStream in = process.getInputStream();BufferedReader bs = new BufferedReader(new InputStreamReader(in));List<String> list = new ArrayList<String>();String result = null;while ((result = bs.readLine()) != null) {System.out.println("job result [" + result + "]");list.add(result);}in.close();process.destroy();return list;}
}
總結
以上是生活随笔為你收集整理的java执行linux脚本 并返回其执行结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工作流实战_04_flowable 流程
- 下一篇: linux 其他常用命令