Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deciding between Apache Commons exec or ProcessBuilder

Tags:

I am trying to decide as to whether to use ProcessBuilder or Commons exec,

My requirements are that I am simply trying to create a daemon process whose stdout/stdin/stderr I do not care about. In addition I want to execute a kill to destroy this process when the time comes.

I am using Java on Linux.

I know that both have their pains and pitfalls (such as being sure to use separate thread to swallow streams can lead to blocking or deadlocks, and closing the streams so not to leave open files hanging around) and wanted to know if anyone had suggestions one way or the other as well as any good resources to follow.

like image 530
Traker Avatar asked Jun 15 '10 19:06

Traker


Video Answer


1 Answers

The ZT Process Executor library is an alternative to Apache Commons Exec. It has functionality to run commands, capture their output, set timeouts, verify program exit status etc.

I have used it a little and I find it well-designed reasonably well-documented.

Example from the documentation

Executing a command, pumping the stderr to a logger, returning the output as UTF8 string.

 String output = new ProcessExecutor().command("java", "-version")     .redirectError(Slf4jStream.of(getClass()).asInfo())     .readOutput(true).execute()     .outputUTF8(); 

Its documentation lists the following advantages over Commons Exec:

  • Improved handling of streams
  • Reading/writing to streams
  • Redirecting stderr to stdout
  • Improved handling of timeouts
  • Improved checking of exit codes
  • Improved API
  • One liners for quite complex usecases
  • One liners to get process output into a String
  • Access to the Process object available
  • Support for async processes ( Future )
  • Improved logging with SLF4J API
  • Support for multiple processes
like image 99
Lii Avatar answered Sep 19 '22 17:09

Lii