Ostermillerutil Java Utilities Helper Methods for Runtime.exec - com.Ostermiller.util Java Utilities

Utilities to make executing other programs from Java easier.

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.


License

OstermillerUtil Java Utilities Copyright (c) 2001-2020 by Stephen Ostermiller and other contributors

The OstermillerUtils library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

License FAQs - Why GPL? How about the LGPL or something else?