Capture the output of an external program in JAVA

前端 未结 2 1793
说谎
说谎 2021-01-12 04:00

I\'m trying to capture output of an external program using java but I can\'t.

I have the code to show it, but not to put it into a variable.

I will use, for

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 04:19

    Use Apache Commons Exec, it will make your life much easier. Check the tutorials for information about basic usage. To read the command line output after obtaining an executor object (probably DefaultExecutor), create an OutputStream to whatever stream you wish (i.e a FileOutputStream instance may be, or System.out), and:

    executor.setStreamHandler(new PumpStreamHandler(yourOutputStream));
    

提交回复
热议问题