Runtime class Understanding
While we know that when writing Java programs, only the thread concept relies on the JVM process, but the API provides the Runtime class, (every Java application has a single instance of class Runtime That allows the application to interface with the environment in which the application is running. The current runtime can is obtained from the GetRuntime method. An application cannot create it own instance of this class.) enables child processes to be used to execute shell scripts through Getruntime.exec (). The principle is: fork a child process from the current virtual machine process, then execute the command with the new process and then exit. So when there are too many of these scenarios, there are a lot of processes that can become a problem, so do not use the Java API to do so. Understanding: The sub-process of course has the concept of plumbing, so clear this point, you can get inputstream/outputstream to do some useful operations.
The following is a simple example:
Import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstreamreader;//executes the specified String command in a separate Process.public class Testruntime {public static void main (string[] args) throws IOException { Runtime r = Runtime.getruntime (); Process p = r.exec ("Man ls"); System.out.println (P.isalive ()); BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream ())); String Res, while ((res = Br.readline ()) = null) {System.out.println (res);}}}
Results:
Runtime class Understanding