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