Creating an operating system process with Java

Source: Internet
Author: User

Example code:

public void Start () throws Exception {String separator = system.getproperty ("File.separator"); String classpath = System.getproperty ("Java.class.path"); String Path = System.getproperty ("Java.home")                + separator + "Bin" + separator + "Java"; Processbuilder Processbuilder =                 new Processbuilder (Path, "-CP",                 classpath,                 AnotherClassWithMainMethod.class.getName ());
Processbuilder.redirecterrorstream (TRUE); Process process = Processbuilder.start ();p rocess.waitfor ();}

Above, Processbuilder start the process more flexible, can set parameters such as Classpath.

Where the Redirecterrorstream method, if the value is set to True, the standard error will be merged with the standard output, which makes it easier to correlate the error message and the corresponding output. In this case, the merged data can be read from the stream returned by Process.getinputstream (), and the stream read returned from Process.geterrorstream () will reach the end of the file directly. Conversely, the standard output and error output of a subprocess are sent to two separate streams that can be accessed through the Process.getinputstream () and Process.geterrorstream () methods.

In addition, three pipe connections are established between the JVM and the new process: standard input, standard output, and standard error stream. Assuming that the program continues to write data to the standard output stream and the standard error stream, and the JVM does not read it, it will not be able to continue writing the data when the buffer is full, eventually causing blocking. The Waitfor method is to wait until the process represented by this process object terminates before the method returns.

Http://stackoverflow.com/questions/3263130/processbuilder-start-another-process-jvm-howto

Creating an operating system process with Java

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.