Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any Java tool to communicate with an external CLI?

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!

like image 405
Alexander Eliseyev Avatar asked Mar 07 '26 11:03

Alexander Eliseyev


1 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).

like image 181
Óscar López Avatar answered Mar 10 '26 01:03

Óscar López