Java执行Linux命令并等待结果返回

public class JavaExecuteLinuxCommandAwaitReturn {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        try {
            java.lang.Runtime runtime = java.lang.Runtime.getRuntime();
            java.lang.ProcessBuilder processBuilder = new java.lang.ProcessBuilder("ps", "-aux");
            processBuilder.redirectErrorStream(true);
            java.lang.Process process = processBuilder.start();
            int waitFor = process.waitFor();
            java.io.InputStream inputStream = process.getInputStream();
            java.io.InputStreamReader inputStreamReader = new java.io.InputStreamReader(inputStream, "UTF-8");
            java.io.BufferedReader bufferedReader = new java.io.BufferedReader(inputStreamReader);
            java.lang.String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (java.io.IOException ioException) {
            ioException.printStackTrace();
        } catch (java.lang.InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
    }
}
 

你可能感兴趣的:(java,jvm,servlet)