External program from our Java program

后端 未结 5 667
半阙折子戏
半阙折子戏 2020-12-11 11:55

How can I write a program in Java that will execute another program? Also, the input of that program should be given from our program and the output of that program should b

5条回答
  •  鱼传尺愫
    2020-12-11 12:48

    The API that Java offers for this is the ProcessBuilder. It is relatively straightforward to set working directory and pass parameters.

    What is a little tricky is passing STDIN and reading STDERR and STDOUT, at least for non-trivial sizes thereof, because you need to start seperate threads to make sure the respective buffers get cleared. Otherwise the application that you called might block until it can write more output, and if you also wait for that process to finish (without making sure that STDOUT gets read), you will deadlock.

提交回复
热议问题