- 论坛徽章:
- 0
|
siberfei 发表于 2012-11-19 21:58 ![]()
回复 4# darrenlee3
我刚才试了一下,jvm拦截了所有stdout/stderr
.exec()返回了Process对象,从中得到inpustream,另启线程输出即可
这时我刚才测试的代码:- /**
- *
- */
- package darrenlee.test.cu;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- /**
- * @author darrenlee
- *
- */
- public class Command implements Runnable {
- private InputStream in;
- public Command(InputStream in) {
- this.in = in;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try {
- Process p = Runtime.getRuntime().exec("ping -c 10 127.0.0.1");
- //Process p = Runtime.getRuntime().exec("ifconfig");
- InputStream in = p.getInputStream();
- Command r = new Command(in);
- new Thread(r).start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(in));
- try {
- String line = bufferedReader.readLine();
- while (line != null) {
- System.out.println(line);
- line = bufferedReader.readLine();
- }
- bufferedReader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码 |
|