
com.Ostermiller.util.ExecHelper.bte Maven / Gradle / Ivy
<%bte.doc super="item.bte" %>
<%bte.tpl name=pageTitle%>Helper Methods for Runtime.exec<%/bte.tpl%>
<%bte.tpl name=description%>Java libraries to make executing external applications easier.<%/bte.tpl%>
<%bte.tpl name=keywords%>exec, exec helper, runtime.exec<%/bte.tpl%>
<%bte.tpl name=topcontent%>
Utilities to make executing other programs from Java easier.
<%/bte.tpl%>
<%bte.tpl name=content%>
Example
// Read a file using cat
String file = ExecHelper.exec(
"cat",
"myfile.txt"
).getOutput();
// Similar to Perl or bash back ticks
String hello = ExecHelper.execUsingShell(
"echo 'Hello World'"
).getOutput();
The Runtime.exec method is particularly difficult to use to call external applications to do data processing compared to such functionality in other languages. For example in Perl and bash, the target can be placed in back ticks and the standard output is placed directly into a variable. To achieve this functionality in Java, one would need to use Runtime.exec, get the process, read from both the output and the error streams, and buffer the data into a String yourself. Or this library can be used.
This class contains convenience methods to call Runtime.exec with a given command. The methods block until the process exits and all the data returned by the command is buffered and returned to the application in a ExecHelper object. The object has three fields: standard output, standard input, and the exit status.
<%/bte.tpl%>
<%bte.tpl name=linkExecHelper%>Exec Helper
<%/bte.tpl%>
<%/bte.doc%>