Package gnu.dtools.ritopt
Class SimpleProcess
- java.lang.Object
-
- java.lang.Process
-
- gnu.dtools.ritopt.SimpleProcess
-
public class SimpleProcess extends java.lang.ProcessA SimpleProcess is used to execute a shell process, and redirect an input stream to the processes' standard input, as well as redirect the processes' standard output/error to an output stream. The processes is multithreaded to prevent deadlock.The example below demonstrates the use of this class.
class ExecuteProcess { public static void main( String args[] ) { if ( args.length > 0 ) { String processName = args[ 0 ]; try { SimpleProcess process = new SimpleProcess( Runtime.getRuntime.exec( processName ) ); ); int exitStatus = process.waitFor(); System.out.println( "The process ran successfully" + " with an exit status of " + exitStatus + "." ); } catch ( Exception e ) { System.out.println( "The process was not successful. " + " Reason: " + e.getMessage() ); } } else { System.err.println( "Please specify a command" ); } } }
Copyright (C) Damian Ryan Eads, 2001. All Rights Reserved. ritopt is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ritopt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ritopt; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
-
Constructor Summary
Constructors Constructor Description SimpleProcess(java.lang.Process process)Constructs a SimpleProcess, redirecting System.in to the its standard input, System.out to its standard output, and System.err to its standard error.SimpleProcess(java.lang.Process process, java.io.InputStream processInput, java.io.PrintStream yourOutput, java.io.PrintStream yourError)Constructs a SimpleProcess, initializing it with the streams passed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddestroy()Destroys the target process.intexitValue()Returns the target processes' exit value.java.io.InputStreamgetErrorStream()Returns the standard error of this process.java.io.InputStreamgetInputStream()Returns the standard output of this process.java.io.OutputStreamgetOutputStream()Returns the standard input of this process.intwaitFor()Begin redirecting the streams passed.
-
-
-
Constructor Detail
-
SimpleProcess
public SimpleProcess(java.lang.Process process) throws java.io.IOExceptionConstructs a SimpleProcess, redirecting System.in to the its standard input, System.out to its standard output, and System.err to its standard error.- Throws:
java.io.IOException
-
SimpleProcess
public SimpleProcess(java.lang.Process process, java.io.InputStream processInput, java.io.PrintStream yourOutput, java.io.PrintStream yourError) throws java.io.IOExceptionConstructs a SimpleProcess, initializing it with the streams passed.- Parameters:
process- The target process.processInput- The stream that is redirected to the processes' standard input.yourOutput- The stream to redirect the processes's standard output.yourError- The stream to redirect the processes's standard input.- Throws:
java.io.IOException
-
-
Method Detail
-
getOutputStream
public java.io.OutputStream getOutputStream()
Returns the standard input of this process.- Specified by:
getOutputStreamin classjava.lang.Process- Returns:
- The standard input of this process.
-
getInputStream
public java.io.InputStream getInputStream()
Returns the standard output of this process.- Specified by:
getInputStreamin classjava.lang.Process- Returns:
- The standard output of this process.
-
getErrorStream
public java.io.InputStream getErrorStream()
Returns the standard error of this process.- Specified by:
getErrorStreamin classjava.lang.Process- Returns:
- The standard error of this process.
-
waitFor
public int waitFor() throws java.lang.InterruptedExceptionBegin redirecting the streams passed. This method should be invoked immediately after execution of a simple process to prevent thread deadlock.- Specified by:
waitForin classjava.lang.Process- Returns:
- The exit status of the target process.
- Throws:
java.lang.InterruptedException
-
exitValue
public int exitValue()
Returns the target processes' exit value.- Specified by:
exitValuein classjava.lang.Process- Returns:
- This processes' exit value.
-
destroy
public void destroy() throws java.lang.IllegalThreadStateExceptionDestroys the target process.- Specified by:
destroyin classjava.lang.Process- Throws:
java.lang.IllegalThreadStateException
-
-