2017年11月13日 星期一

Java 執行 linux command

    private String executeCommand(String command) throws Exception {
        StringBuffer runShellResult = new StringBuffer();
        Process p = Runtime.getRuntime().exec(command);
        p.waitFor();

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            runShellResult.append(line + "\n");
        }
       
        return runShellResult.toString();

    }