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执行Linux命令并等待结果返回
最新推荐文章于 2024-04-11 09:47:19 发布