package threaddemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ProcessDemo {

    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"ls","-l", "/home/admin"});

            // 读取标准输出
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = stdInput.readLine()) != null) {
                System.out.println("标准输出: " + line);
            }

            // 读取错误输出
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = stdError.readLine()) != null) {
                System.out.println("错误输出: " + line);
            }

            // 等待进程结束
            int exitCode = p.waitFor();

            System.out.println("进程结束，退出码: " + exitCode);
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
