org.knowceans.util
Class ExternalProcess

java.lang.Object
  extended by org.knowceans.util.ExternalProcess

public class ExternalProcess
extends java.lang.Object

Class to start an external process and connect the three standard streams. The process can be run using the constructor, then optionally connection its output streams stdout and stdin with Java InputStreams using getStreamFromStdout and getStreamFromStderr, then calling waitfor() to wait until the process has finished. Any stdin of the external program can be assigned via the last arg to the constructor; if stdin should be from a String, use StringBufferInputStream.
If getStreamFromStdout and getStreamFromStderr are used, they should be buffered and run in a separate thread if possible. If they are not explicitly requested by the caller, internal threads are used that collect the output in a buffer that can be requested from the caller class via getCompleteStdout and getCompleteStderr. Additionally, the threads display the process output in Java's stdout and stderr.
TODO: stdin does not work yet.

Author:
heinrich

Constructor Summary
ExternalProcess(java.lang.String command, java.lang.String[] options, java.lang.String[] environment, java.io.InputStream stdin, java.io.File workDir)
          Initialise the external program.
 
Method Summary
 java.lang.String getCompleteStderr()
          get output from the stderr buffer thread.
 java.lang.String getCompleteStdout()
          get output from the stdout buffer thread.
 java.io.InputStream getStreamFromStderr()
          Stderr from the process is processed by the user.
 java.io.InputStream getStreamFromStdout()
          Stdout from the process is processed by the user.
static void main(java.lang.String[] args)
          test method to run an external program
 int waitFor()
          should be called after the process has been started (using the constructor)
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExternalProcess

public ExternalProcess(java.lang.String command,
                       java.lang.String[] options,
                       java.lang.String[] environment,
                       java.io.InputStream stdin,
                       java.io.File workDir)
                throws java.lang.Exception
Initialise the external program. If a String is to be used as an input to the process, StringReader can be used.

Parameters:
command - required system-visible command
options - optional, set null or zero-length if unwanted
environment - optional, set null or zero-length if unwanted
stding - stream to connect to stdin of the process, null if not wanted
workDir - to run the process in
Throws:
java.lang.Exception
Method Detail

waitFor

public int waitFor()
            throws java.lang.InterruptedException
should be called after the process has been started (using the constructor)

Returns:
Throws:
java.lang.InterruptedException

getStreamFromStdout

public java.io.InputStream getStreamFromStdout()
Stdout from the process is processed by the user. To run stable, the stdout stream should be buffered and its processing run in its own thread.

Returns:
the stream from stdout of the process

getStreamFromStderr

public java.io.InputStream getStreamFromStderr()
Stderr from the process is processed by the user. see getStreamFromStdout()

Returns:
the stream from stderr of the process

getCompleteStdout

public java.lang.String getCompleteStdout()
get output from the stdout buffer thread.

Returns:

getCompleteStderr

public java.lang.String getCompleteStderr()
get output from the stderr buffer thread.


main

public static void main(java.lang.String[] args)
test method to run an external program

Parameters:
args -
Throws:
java.lang.Exception