verinec.util
Class ProgramExec

java.lang.Object
  extended by verinec.util.AbstractProgramExec
      extended by verinec.util.ProgramExec

public class ProgramExec
extends AbstractProgramExec

Wrapper methods for execution of native programs via the Runtime.exec().

These methods provide for simpler program execution, allowing to redirect program output or input to files (the < / > parameters in a shell), or retrieving the output as a stream. They allow to set a timeout after which program execution is interrupted.

You can execute a program and wait for its result, or execute it asynchronous, that is in a separate thread without waiting for it to finish.

Version:
$Revision: 859 $
Author:
david.buchmann at unifr.ch

Nested Class Summary
 
Nested classes/interfaces inherited from class verinec.util.AbstractProgramExec
AbstractProgramExec.StreamGobbler, AbstractProgramExec.TimeoutWatcher
 
Field Summary
 
Fields inherited from class verinec.util.AbstractProgramExec
cwd, timedOut
 
Constructor Summary
ProgramExec(String commandline, File cwd, InputStream stdin, OutputStream stdout, OutputStream stderr)
          Prepare a program execution.
 
Method Summary
static int execSync(String commandline, File cwd, InputStream stdin, OutputStream stdout, OutputStream stderr, int timeout)
          Execute a program and wait for it to terminate.
protected  Process startProcess(String commandline, File cwd)
          Instantiate a process using Runtime.
 
Methods inherited from class verinec.util.AbstractProgramExec
abortExecution, execAsync, execSync, exitValue, hasFinished
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProgramExec

public ProgramExec(String commandline,
                   File cwd,
                   InputStream stdin,
                   OutputStream stdout,
                   OutputStream stderr)
Prepare a program execution. Afterwards, you can either run the program synchronous or asynchronous.

The input for the program can be given as an InputStream that will be piped to the program as stdin. If you have a file as input, you can use java.io.FileInputStream. stdin can be null, in which case no input is given to the program.

The output can be pushed into OutputStreams for later treatment. If both streams are null, output is discarded. If the stdout stream is null only, errors are written into stderr, if stderr is null, both out and err written into stdout. Again, you can use FileOutputStream to write into files. The streams will be closed when the program execution is finished.

It is recommended to wrap the output streams with a BufferedOutputStream to avoid delays.

Parameters:
commandline - Name of the executable with all parameters. (Do not forget full path if executable is not in the path for programs).
cwd - The current working directory directory for the new process (defaults to the system property user.dir if null).
stdin - A Stream that will be fed to the process as input (ignored if null).
stdout - A Stream to write the program output into (discarded if null).
stderr - A Stream to write the program error output into (redirected to stdout if null).
Method Detail

execSync

public static int execSync(String commandline,
                           File cwd,
                           InputStream stdin,
                           OutputStream stdout,
                           OutputStream stderr,
                           int timeout)
                    throws InterruptedException,
                           VerinecException
Execute a program and wait for it to terminate.

The input for the program can be given as an InputStream that will be piped to the program as stdin. If you have a file as input, you can use java.io.FileInputStream. stdin can be null, in which case no input is given to the program.

The output can be pushed into OutputStreams for later treatment. If both streams are null, output is discarded. If the stdout stream is null only, errors are written into stderr, if stderr is null, both out and err written into stdout. Again, you can use FileOutputStream to write into files. The streams will be closed when the program execution is finished.

It is recommended to wrap the output streams with a BufferedOutputStream to avoid delays.

Parameters:
commandline - Name of the executable with all parameters. (Do not forget full path if executable is not in the path for programs).
stdin - A Stream that will be fed to the process as input (ignored if null).
stdout - A Stream to write the program output into (discarded if null).
stderr - A Stream to write the program error output into (redirected to stdout if null).
cwd - The current working directory directory for the new process (defaults to the system property user.dir if null).
timeout - The time in seconds after which the execution should be interrupted (0 for no timeout).
Returns:
The exit code of the program.
Throws:
InterruptedException - If the timeout occurs or the process is otherwise interrupted.
VerinecException - If other errors occur.

startProcess

protected Process startProcess(String commandline,
                               File cwd)
                        throws VerinecException
Instantiate a process using Runtime. It will have the same environment as System.getenv()

Specified by:
startProcess in class AbstractProgramExec
Parameters:
commandline - The command line to execute.
cwd - The current working directory for the command.
Returns:
A running process.
Throws:
VerinecException - If the command is not found.

Copyright © 2005 Verinec, DIUF