58

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?

Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238

8 Answers8

48

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
  • 268,207
  • 37
  • 334
  • 440
  • 1
    I noticed that if the command gives a lot of output the code flow will continue before the Gobblers are done outputting. Need to call .join() on them before returning. – Zitrax Oct 20 '11 at 14:27
  • _Extremely_ effective and simple method. One thing to note though is that the `cmd` array initialization in the Main method seems to be a little dated for Windows 7. Add a final "else" clause that initializes the cmd array to the NT style even if the other `else if( osName.equals( "Windows NT" )` comes back false. – Eternal Rubyist Oct 24 '11 at 22:08
  • Is the solution with SteamGobbler is only for a Windows server? If I am using Unix- it wouldn't happen? – Dejell Nov 20 '11 at 13:42
  • In Windows, it is either *command.exe* or *cmd.exe*. If you use Linux, you have to specify your choice of command interpreter like bash, etc. – ee. Feb 17 '12 at 01:06
  • @Odelya - I don't believe this is OS-specific – Brian Agnew Mar 30 '12 at 08:13
  • This is not platform specific – Brian Agnew Feb 06 '21 at 12:13
  • The link to the article is broken. – Paul Wintz Mar 15 '21 at 03:41
  • Amended with a new link – Brian Agnew Mar 15 '21 at 16:50
16

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
  • 29,624
  • 9
  • 57
  • 79
7

For processes that don't generate much output, I think this simple solution that utilizes Apache IOUtils is sufficient:

Process p = Runtime.getRuntime().exec("script");
p.waitFor();
String output = IOUtils.toString(p.getInputStream());
String errorOutput = IOUtils.toString(p.getErrorStream());

Caveat: However, if your process generates a lot of output, this approach may cause problems, as mentioned in the Process class JavaDoc:

The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

erwaman
  • 3,307
  • 3
  • 28
  • 29
7

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
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
adrian.tarau
  • 3,124
  • 2
  • 26
  • 29
  • 2
    Why use an external library when the core language has a perfectly suitable alternative? – pauljwilliams May 19 '09 at 13:54
  • 2
    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 May 19 '09 at 16:23
  • 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 May 19 '09 at 16:26
5

This is my helper class been using for years. One small class. It has JavaWorld streamgobbler class to fix JVM resource leaks. Don't know if still valid for JVM6 and JVM7 but does not hurt. Helper can read output buffer for later use.

import java.io.*;

/**
 * Execute external process and optionally read output buffer.
 */
public class ShellExec {
    private int exitCode;
    private boolean readOutput, readError;
    private StreamGobbler errorGobbler, outputGobbler;

    public ShellExec() { 
        this(false, false);
    }

    public ShellExec(boolean readOutput, boolean readError) {
        this.readOutput = readOutput;
        this.readError = readError;
    }

    /**
     * Execute a command.
     * @param command   command ("c:/some/folder/script.bat" or "some/folder/script.sh")
     * @param workdir   working directory or NULL to use command folder
     * @param wait  wait for process to end
     * @param args  0..n command line arguments
     * @return  process exit code
     */
    public int execute(String command, String workdir, boolean wait, String...args) throws IOException {
        String[] cmdArr;
        if (args != null && args.length > 0) {
            cmdArr = new String[1+args.length];
            cmdArr[0] = command;
            System.arraycopy(args, 0, cmdArr, 1, args.length);
        } else {
            cmdArr = new String[] { command };
        }

        ProcessBuilder pb =  new ProcessBuilder(cmdArr);
        File workingDir = (workdir==null ? new File(command).getParentFile() : new File(workdir) );
        pb.directory(workingDir);

        Process process = pb.start();

        // Consume streams, older jvm's had a memory leak if streams were not read,
        // some other jvm+OS combinations may block unless streams are consumed.
        errorGobbler  = new StreamGobbler(process.getErrorStream(), readError);
        outputGobbler = new StreamGobbler(process.getInputStream(), readOutput);
        errorGobbler.start();
        outputGobbler.start();

        exitCode = 0;
        if (wait) {
            try { 
                process.waitFor();
                exitCode = process.exitValue();                 
            } catch (InterruptedException ex) { }
        }
        return exitCode;
    }   

    public int getExitCode() {
        return exitCode;
    }

    public boolean isOutputCompleted() {
        return (outputGobbler != null ? outputGobbler.isCompleted() : false);
    }

    public boolean isErrorCompleted() {
        return (errorGobbler != null ? errorGobbler.isCompleted() : false);
    }

    public String getOutput() {
        return (outputGobbler != null ? outputGobbler.getOutput() : null);        
    }

    public String getError() {
        return (errorGobbler != null ? errorGobbler.getOutput() : null);        
    }

//********************************************
//********************************************    

    /**
     * StreamGobbler reads inputstream to "gobble" it.
     * This is used by Executor class when running 
     * a commandline applications. Gobblers must read/purge
     * INSTR and ERRSTR process streams.
     * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
     */
    private class StreamGobbler extends Thread {
        private InputStream is;
        private StringBuilder output;
        private volatile boolean completed; // mark volatile to guarantee a thread safety

        public StreamGobbler(InputStream is, boolean readStream) {
            this.is = is;
            this.output = (readStream ? new StringBuilder(256) : null);
        }

        public void run() {
            completed = false;
            try {
                String NL = System.getProperty("line.separator", "\r\n");

                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                while ( (line = br.readLine()) != null) {
                    if (output != null)
                        output.append(line + NL); 
                }
            } catch (IOException ex) {
                // ex.printStackTrace();
            }
            completed = true;
        }

        /**
         * Get inputstream buffer or null if stream
         * was not consumed.
         * @return
         */
        public String getOutput() {
            return (output != null ? output.toString() : null);
        }

        /**
         * Is input stream completed.
         * @return
         */
        public boolean isCompleted() {
            return completed;
        }

    }

}

Here is an example reading output from .vbs script but similar works for linux sh scripts.

   ShellExec exec = new ShellExec(true, false);
   exec.execute("cscript.exe", null, true,
      "//Nologo",
      "//B",            // batch mode, no prompts
      "//T:320",        // timeout seconds
      "c:/my/script/test1.vbs",  // unix path delim works for script.exe
      "script arg 1",
      "script arg 2",
   );
   System.out.println(exec.getOutput());
Whome
  • 10,181
  • 6
  • 53
  • 65
2

VerboseProcess utility class from jcabi-log can help you:

String output = new VerboseProcess(
  new ProcessBuilder("executable with output")
).stdout();

The only dependency you need:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-log</artifactId>
  <version>0.7.5</version>
</dependency>
yegor256
  • 102,010
  • 123
  • 446
  • 597
1

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

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
1

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
  • 11,667
  • 45
  • 59