views:

2006

answers:

6

When experiencing networking problems on client machines, I'd like to be able to run a few command lines and email the results of them to myself.

I've found Runtime.exec will allow me to execute arbitrary commands, but Collecting the results in a String is more interesting.

I realize I could redirect output to a file, and then read from the file, but my spidey sense is telling me there's a more elegant way of doing it.

Suggestions?

+3  A: 

Use ProcessBuilder. After calling start() you'll get a Process object from which you can get the stderr and stdout streams.

UPDATE: ProcessBuilder gives you more control; You don't have to use it but I find it easier in the long run. Especially the ability to redirect stderr to stdout which means you only have to suck down one stream.

basszero
+2  A: 

Runtime.exec() returns a Process object, from which you can extract the output of whatever command you ran.

Visage
+4  A: 

You need to capture both the std out and std err in the process. You can then write std out to a file/mail or similar.

See this article for more info, and in particular note the StreamGobbler mechanism that captures stdout/err in separate threads. This is essential to prevent blocking and is the source of numerous errors if you don't do it properly!

Brian Agnew
Worked like a charm.
Allain Lalonde
A: 

Have a look at this article.

kgiannakakis
A: 

Using Runtime.exec gives you a process. You can these use getInputStream to get the stdout of this process, and put this input stream into a String, through a StringBuffer for example.

Valentin Rocher
A: 

Use Plexus Utils, it is used by Maven to execut all external processes.

Commandline commandLine = new Commandline();
    commandLine.setExecutable(executable.getAbsolutePath());

    Collection<String> args = getArguments();

    for (String arg : args) {
        Arg _arg = commandLine.createArg();
        _arg.setValue(arg);
    }

WriterStreamConsumer systemOut = new WriterStreamConsumer(console);
    WriterStreamConsumer systemErr = new WriterStreamConsumer(console);

returnCode = CommandLineUtils.executeCommandLine(commandLine, systemOut, systemErr, 10);
            if (returnCode != 0) {
   // bad
} else {
  // good
}
adrian.tarau
Why use an external library when the core language has a perfectly suitable alternative?
Visage
There are some differences which may not be seen in the beginning.1. if you call Process.waitFor() it will block, this means you MUST read the the process output otherwise the process will wait until the output buffer(console output) will be available. if you choose this path(getting the output yourself) you must not use waitFor().2. if you poll, then you have to add yourself code to handle that while you're waiting to read the output.The purpose of libraries like Plexus Utils - 246k- is to help you avoid reinventing the wheel over an over again :)
adrian.tarau
Ant does the same thing, you can use it if you want, there is a core task which can be called(with a proper initialized ant context) to perform this task, but I prefer Plexus Utils since is smaller(you can even strip out everything except the cli package, which means you will have less than 50k), dedicated and proof to be stable(since is included in Maven 2)
adrian.tarau