执行命令部分
try {
String command = "java -version";
Process p = Runtime.getRuntime().exec(command);
StreamCaptureThread errorStream = new StreamCaptureThread(p.getErrorStream());
StreamCaptureThread outputStream = new StreamCaptureThread(p.getInputStream());
new Thread(errorStream).start();
new Thread(outputStream).start();
p.waitFor();
result = command + "\n" + outputStream.output.toString()
+ errorStream.output.toString();
System.out.print(result);
} catch (Exception e) {
e.printStackTrace();
}
StreamCaptureThread
private class StreamCaptureThread implements Runnable {
InputStream stream;
StringBuilder output;
public StreamCaptureThread(InputStream stream) {
this.stream = stream;
this.output = new StringBuilder();
}
public void run() {
try {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(this.stream));
String line = br.readLine();
while (line != null) {
if (line.trim().length() > 0) {
output.append(line).append("\n");
}
line = br.readLine();
}
} finally {
if (stream != null) {
stream.close();
}
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
}
执行命令部分 try { String command = "java -version"; Process p = Runtime.getRuntime().exec(command); StreamCaptureThread errorStream = n