util
Class Shell

java.lang.Object
  extended by util.Shell

public class Shell
extends Object

Utility class which acts like a system shell manipulable from Java code.


Field Summary
protected  File cwd
          The current working directory.
protected  int lastExitValue
          The exit value of the last command executed.
protected  PrintStream stderr
          The stream to use for standard error.
protected  InputStream stdin
          The stream to use for standard input.
protected  PrintStream stdout
          The stream to use for standard output.
 
Constructor Summary
Shell()
           
 
Method Summary
 Shell chdir(File dir)
          Change directory.
 Process fork(String... cmdLine)
          Run a process within this shell, in the background.
 File getCwd()
          Get the current directory.
 int getLastExitValue()
           
 PrintStream getStdErr()
           
 InputStream getStdIn()
           
 PrintStream getStdOut()
           
 int run(String... cmdLine)
          Run a process within this shell.
 int runClass(Class<?> cls, String... args)
          Run a Java class as a process within this shell.
 Shell setStdErr(PrintStream stderr)
           
 Shell setStdIn(InputStream stdin)
           
 Shell setStdOut(PrintStream stdout)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cwd

protected File cwd
The current working directory.


stdout

protected PrintStream stdout
The stream to use for standard output.


stderr

protected PrintStream stderr
The stream to use for standard error.


stdin

protected InputStream stdin
The stream to use for standard input.


lastExitValue

protected int lastExitValue
The exit value of the last command executed.

Constructor Detail

Shell

public Shell()
Method Detail

getCwd

public File getCwd()
Get the current directory. Analogous to pwd.

Returns:
The current working directory.

chdir

public Shell chdir(File dir)
            throws IOException
Change directory. Analogous to cd.

Parameters:
dir - The directory to change to.
Returns:
this, for chaining.
Throws:
IOException - If cwd is not a directory or does not exist.

getStdErr

public PrintStream getStdErr()
Returns:
The stream to be used as standard error by processes spawned by this shell.

setStdErr

public Shell setStdErr(PrintStream stderr)
Parameters:
stderr - A stream which will receive anything a child process sends to its standard error stream. By default this will be System.err, that is, the system-wide standard error stream.
Returns:
this, for chaining.

getStdIn

public InputStream getStdIn()
Returns:
The stream to be used as standard input by processes spawned by this shell.

setStdIn

public Shell setStdIn(InputStream stdin)
Parameters:
stdin - A stream to be connected to standard input for child processes. By default this will be System.in, that is, the system-wide standard input stream.
Returns:
this, for chaining.

getStdOut

public PrintStream getStdOut()
Returns:
The stream to be used as standard output by processes spawned by this shell.

setStdOut

public Shell setStdOut(PrintStream stdout)
Parameters:
stdout - A stream which will receive anything a child process sends to its standard output stream. By default this will be System.err, that is, the system-wide standard output stream.
Returns:
this, for chaining.

run

public int run(String... cmdLine)
        throws IOException,
               InterruptedException
Run a process within this shell.

Parameters:
cmdLine - The command line, split into its arguments. No shell-like processing - word splitting, variable substitution, etc. - will be performed.
Returns:
The exit code returned by the program.
Throws:
IOException - If the program cannot be found or run.
InterruptedException - If this thread is interrupted while the program is running.

fork

public Process fork(String... cmdLine)
             throws IOException
Run a process within this shell, in the background.

Parameters:
cmdLine - The command line, split into its arguments. No shell-like processing - word splitting, variable substitution, etc. - will be performed.
Returns:
The process, or null if it could not be started.
Throws:
IOException - If the program cannot be found or run.

runClass

public int runClass(Class<?> cls,
                    String... args)
             throws IOException,
                    InterruptedException
Run a Java class as a process within this shell.

Parameters:
cls - The class to run. Must declare a method with signature public static void main(String[]).
args - Arguments to pass to the class's main method.
Returns:
The exit value returned by the VM.
Throws:
IOException - For any problems relating to I/O (not being able to access the class file, etc.)
InterruptedException - If this thread is interrupted while the class is running.
IllegalArgumentException - If cls does not declare a method with the correct signature.

getLastExitValue

public int getLastExitValue()
Returns:
The exit value of the last process run by this shell.