In Java, we can use two methods to call sub-processes. The simplest method is to execute the java class through the exec method in Runtime. If the execution is successful, this method returns a Process object. If the execution fails, an IOExcetion error is thrown. For example:
Public class Test1 {public static void main (String [] args) throws Exception {System. out. println ("sub-process called successfully! ");}}public class Test1_Exec {public static void main(String[] args) throws Exception {Runtime run = Runtime.getRuntime();run.exec("java Test1");}}If you want to output the output information of a child Process, you can get the output stream of the child Process through getInputStream in Process (output in the child Process, input in the parent Process ), then, output the output stream in the child process from the console of the parent process.
import java.io.*;public class Test_Exec_Out{public static void main(String[] args) throws Exception{Runtime run = Runtime.getRuntime();Process p = run.exec("java Test1"); BufferedInputStream in = new BufferedInputStream(p.getInputStream());BufferedReader br = new BufferedReader(new InputStreamReader(in));String s;while ((s = br.readLine()) != null)System.out.println(s); }}The result shows that the parent process obtains the input of the child process.
We discussed how to get the output information of sub-processes. In addition to output information, there are also input information. Since the sub-process does not have its own console, the input information must be provided by the parent process. We can use the getOutputStream method of Process to provide input information for the child Process (that is, the parent Process inputs information to the child Process, rather than entering information on the console ).
Import java. io. *; public class Test2 {public static void main (String [] args) throws IOException {BufferedReader br = new BufferedReader (new InputStreamReader (System. in); System. out. println ("information entered by the parent process:" + br. readLine ());}}Import Java. io. *; public class test_exec_in {public static void main (string [] ARGs) throws ioexception {runtime run = runtime. getruntime (); process P = run.exe C ("Java Test2"); bufferedwriter BW = new bufferedwriter (New outputstreamwriter (P. getoutputstream (); BW. write ("input information to sub-process"); BW. flush (); BW. close (); // The stream must be closed; otherwise, information cannot be entered into the child process. // system. in. read ();}}
In addition to using the exec method of runtime to create sub-processes, you can also use processbuilder to create sub-processes.
import java.io.*;public class Test_Exec_Out2{public static void main(String[] args) throws IOException{ProcessBuilder pb = new ProcessBuilder("java", "Test1");Process p = pb.start();}}In the establishment of sub-processes, processbuilder and runtime are similar. Different processbuilder uses the START () method to start the process, while runtime uses the exec method to start the process. After the process is obtained, their operations are exactly the same.