Runtime class understanding

Source: Internet
Author: User

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

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.