Brief analysis of Java.lang.Process class

Source: Internet
Author: User

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 ()
Kill 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, if necessary, 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, please refer to the "Analysis Java.lang.ProcessBuilder Class" article) 2, Runtime.exec () method to create a native process and return to the process An instance of the child class. For details, please refer to the article "Analysis of Java.lang.Runtime class".Iv. Examples
ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader; Public Final classCmdtoolkit {Private StaticLog log = Logfactory.getlog (Cmdtoolkit.class); PrivateCmdtoolkit () {}/*** Read the output of the control command * *@paramcmd command *@paramIsprettify 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, *@returnoutput of the control command *@throwsIOException*/          Public StaticString Readconsole (string cmd, Boolean isprettify)throwsIOException {stringbuffer cmdout=NewStringBuffer (); Log.info ("Execute command:" +cmd); Process Process= Runtime.getruntime (). exec (CMD);//execute a system commandInputStream FIS =Process.getinputstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (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 of executing a system command is: \ n" +cmdout.tostring ()); returncmdout.tostring (). Trim (); } }

Brief analysis of Java.lang.Process class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.