8 Answers
8

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!

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.
–
ZitraxOct 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.
–
nomizzzOct 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?
–
DejelNov 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 1:06

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.

Why use an external library when the core language has a perfectly suitable alternative?
–
PaulJWilliamsMay 19 '09 at 13:54

1

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.tarauMay 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.tarauMay 19 '09 at 16:26

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.

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.