I need a way to communicate with an external tool which has a Command Line Interface from my Java application. Is there any handy tool/lib to make it less painful? Believe me, I've googled enough. Thank you!
-
http://stackoverflow.com/questions/2417495/java-cli-ui-design-frameworks-or-libraries. Internet search for "Java shell" will yield a couple of possibilities. Pure CLI for embedding might be extracted from those projects too. – Joop Eggen Apr 14 '12 at 18:44
-
@JoopEggen I don't think that's what the OP wants, he needs to communicate with an external CLI tool, not to implement a CLI – Óscar López Apr 14 '12 at 18:54
-
1http://stackoverflow.com/questions/3343066/reading-streams-from-java-runtime-exec – Tito Oct 21 '12 at 04:56
2 Answers
You can use Runtime.exec()
for executing external commands of the operating system. Be aware, though, of the gotchas of using it - make sure to read this article first.
If that's too simplistic for your needs, take a look at ProcessBuilder. It's available since Java 1.5, from the release notes:
The new ProcessBuilder class provides a more convenient way to invoke subprocesses than does Runtime.exec. In particular, ProcessBuilder makes it easy to start a subprocess with a modified process environment (that is, one based on the parent's process environment, but with a few changes).

- 232,561
- 37
- 312
- 386
-
-
@AlexanderEliseyev I updated my answer with another, better alternative – Óscar López Apr 14 '12 at 18:53
-
I need a way to **communicate** with a process, not to start it. I need a tool to ease the handling of the requests/responses while talking to the external CLI. – Alexander Eliseyev Apr 14 '12 at 19:01
I've ended up using the process handler (more specifically, the com.intellij.execution.process.OSProcessHandler) from Intellij IDEA platform API + writing my own small command-based framework where each command knowns how to execute itself (write) and parse the response from a process (read).

- 437
- 4
- 11