You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Cris Perdue <cr...@perdues.com> on 2004/09/02 01:18:41 UTC
[io] Re: ProcessUtils and support classes offer
Hello Matthew,
Thanks for checking out the ProcessUtils documentation! Unfortunately
some of my commons-dev digest emails have been getting sorted into my
Spam folder, so your reply only showed up as I went to fix up spam
processing. Hopefully I will get the rest of them.
Matthew Hawthorne wrote:
> Cris Perdue wrote:
>
> > The purpose of the main ProcessUtils class is to make it simpler to
call
> > external programs and receive the results back. It was inspired
> > especially by the TCL exec function and also resembles the Perl
> > backtick operator. A simple call to "df" using the simplest form
> > of call could look something like:
> >
> > String filestats = ProcessUtils.exec(new String[] {"df"});
> >
> > The package handles program output to "standard error" and nonzero
exit codes.
>
>
> This may be a good fit for commons-io
> (http://jakarta.apache.org/commons/io/). A class like ProcessUtils
> seems interesting, but it would definitely need some extensive testing
> on different platforms to make sure it's OK.
>
> Consider submitting your stuff as a Bugzilla enhancement, so that the
> commons-io developers can take a look when they get a chance.
>
> I believe the javadocs state that ProcessUtils throws an exception if
> anything is written to stderr. I would like it if this were
> customizable, since a lot of programs write warnings and such to
> stderr, but still keep on running. Perhaps it could become some type
> of callback mechanism.
Yes, this is a good case to handle. The "dual Writer" version of exec
can handle this and exit status also, e.g.:
StringWriter ow = new StringWriter();
StringWriter ew = new StringWriter();
int status = ProcessUtils.exec(new String[] {"rpm", "-ql",
"somepackage"}, ow, ew);
String output = ow.toString();
It would be simple to allow null values for the Writers and ignore the
output. That would avoid the need for the app to create one or both of
the Writers.
A ProcessUtils.Exception is actually an object with all of the results
of the program. It has a string with all the contents of stdout, a
string with stderr, and an integer exit status. One could make a form
that always returns an object like this and never throws, or one could
pass the object to "exec", which would fill it in and never throw. I
rather like this option. A simple call would look something like:
ProcessUtils.Exception ex = new ProcessUtils.Exception();
String output = ProcessUtils.exec(new String[] {"df"}, ex);
Perhaps a null value for the extra parameter could mean "ignore exit
status and output to stderr". (And by the way in this case I would
rename the Exception and make it a top-level class.) So there are a
couple of additional options I can see.
Thanks also for the suggestions on how to submit this.
Cheers,
Cris