I. Overview
The Process class is an abstract class (all methods are abstract) that encapsulates a Process (that is, an execution program ).
The Process class provides methods to execute processes from Process input, execution output to Process, wait for the Process to complete, check the exit status of the Process, and destroy (kill) the Process.
ProcessBuilder. start () and Runtime.exe c create a local Process and return an instance of the Process subclass. This instance can be used to control the Process and obtain relevant information.
The Process Creation method may not work well for certain processes on the local platform, such as Windows processes, Daemon Processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created sub-process does not have its own terminal or console. All its standard io (namely stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream (), getInputStream (), getErrorStream. The parent process uses these streams to provide the input to the child process and obtain the output from the child process. Some local platforms only provide limited Buffer Sizes for standard input and output streams. If the output stream or input stream of the read/write sub-process fails rapidly, the sub-process may be blocked, A deadlock even occurs.
When there is no more reference to the Process object, instead of deleting the sub-Process, the sub-Process will continue to be executed asynchronously.
Ii. API Preview
Destroy ()
Kill the child process.
ExitValue ()
Returns the export value of the sub-process.
InputStream getErrorStream ()
Obtain the error stream of the sub-process.
InputStream getInputStream ()
Obtain the input stream of the sub-process.
OutputStream getOutputStream ()
Obtain the output stream of the sub-process.
WaitFor ()
This causes the current thread to wait. If necessary, wait until the Process indicated by this Process object has been terminated.
3. There are two methods to create a Process object:
1. Each ProcessBuilder instance manages a process property set. The start () method uses these attributes to create a new Process instance. The start () method can be called repeatedly from the same instance to create a new sub-process with the same or related attributes. (ProcessBuilder: The final class added in JDK 5. For details, see the article "in-depth study of java. lang. ProcessBuilder class.
2、Runtime.exe c () method to create a local Process and return an instance of the Process subclass. For details, see the article "in-depth study of java. lang. Runtime classes.
Iv. Instances
Import org. apache. commons. logging. Log;
Import org. apache. commons. logging. LogFactory;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
/**
* Console Processing Toolbox
*
* @ Author leizhimin 2009-6-25 14:12:14
*/
Public final class development toolkit {
Private static Log log = LogFactory. getLog (logging toolkit. class );
Private plugin Toolkit (){
}
/**
* Read control command output result
*
* @ Param cmd command
* @ Param isPrettify whether to beautify the returned results (line feed). beautification means line feed. By default, line feed is not beautified. If this parameter is null, line feed is not beautified,
* @ Return refers to the output result of the control command.
* @ Throws IOException
*/
Public static String readConsole (String cmd, Boolean ispretexception) throws IOException {
StringBuffer cmdout = new StringBuffer ();
Log.info ("Run Command:" + cmd );
Process process = runtime.getruntime(cmd.exe c (cmd); // execute a system command
InputStream FCM = process. getInputStream ();
BufferedReader br = new BufferedReader (new InputStreamReader (FCM ));
String line = null;
If (ispretiterator = null | ispretiterator ){
While (line = br. readLine ())! = Null ){
Cmdout. append (line );
}
} Else {
While (line = br. readLine ())! = Null ){
Cmdout. append (line). append (System. getProperty ("line. separator "));
}
}
Log.info ("the result after executing the system command is \ n" + cmdout. toString ());
Return cmdout. toString (). trim ();
}
}