Skip to content

执行系统命令

java
public static String execCommand(String command){
    System.out.println(command);
    String[] commands = new String[]{"sh", "-c", command};
    String result = "";
    try{
        Process proc = java.lang.Runtime.getRuntime().exec(commands);
        java.io.InputStreamReader isr = new java.io.InputStreamReader(proc.getInputStream());
        java.io.BufferedReader stdInput = new java.io.BufferedReader(isr);
        String line = null;
        while ((line=stdInput.readLine())!=null) {
            result += line + "\n";
        }
        proc.waitFor();
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println(result);
    return result;
}

Released under the MIT License.