I. Overview
The process class is an abstract class (all methods are abstract) and encapsulates a process (that is, an executing program). The process class provides a way to perform input from the process, perform output to the process, wait for the process to complete, check the exit status of the process, and destroy (kill) the process. The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of the process subclass that can be used to control the process and obtain relevant information.
the method of creating a process may not work well for specific processes on some native platforms, such as native window processes, daemons, win16/dos processes on Microsoft Windows, or shell scripts. The child process created does not have its own terminal or console. All of its standard IO (that is, Stdin,stdout,stderr) operations are redirected to the parent process through three streams (Getoutputstream (), getInputStream (), Geterrorstream ()). The parent process uses these streams to provide input to the child process and to obtain output from the child process. Because some native platforms only provide a limited buffer size for standard input and output streams, it can cause child processes to block and even deadlock if the output stream or input stream of a child process fails quickly. When there are no more references to the process object, instead of deleting the child process, it continues to execute the child process asynchronously. Second, API preview Destroy () kills the child process. Exitvalue () returns the exit value of the child process. InputStream Geterrorstream () gets the error stream for the child process. InputStream getInputStream () gets the input stream of the child process. OutputStream Getoutputstream () gets the output stream of the child process. WaitFor () causes the current thread to wait and, if necessary, waits until the process represented by that process object has been terminated.three, how to create a process object, there are generally two ways:1. Each Processbuilder instance manages a set of process properties. The start () method uses these properties to create a new Process instance. The start () method can be called repeatedly from the same instance to create a new child process with the same or related properties. (Processbuilder This is the newly added final class in JDK5, see the article "in-depth study of the Java.lang.ProcessBuilder class" for details. 2. The Runtime.exec () method creates a native process and returns an instance of the process subclass. See the article "in-depth study of Java.lang.Runtime class" for details.Iv. ExamplesImport 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
*/
PublicFinalClass Cmdtoolkit {
PrivateStatic log log = Logfactory.getlog (Cmdtoolkit.Class);
Private Cmdtoolkit () {
}
/**
* Output of the Read control command
*
* @param cmd command
* @param isprettify Returns whether the result is beautified (line wrapping), beautification means line wrapping, default does not beautify, when this parameter is null also does not beautify,
* Output of @return Control command
* @throws IOException
*/
Publicstatic string Readconsole (string cmd, Boolean isprettify)Throws IOException {
StringBuffer cmdout =New StringBuffer ();
Log.info ("Execute command:" + cmd);
Process process = Runtime.getruntime (). exec (CMD);Execute a system command
InputStream FIS = Process.getinputstream ();
BufferedReader br =New BufferedReader (New InputStreamReader (FIS));
String line =Null
if (isprettify = =null | | Isprettify) {
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 ();
}
}
In-depth study of java.lang.Process class