ProcessBuilder类的start()方法 (ProcessBuilder Class start() method)
start() method is available in java.lang package.
start()方法在java.lang包中可用。
start() method is used to kick or start a new process using the set attributes of process builder and it checks the calling command is valid operating system command and check system dependent command invalid commands.
start()方法用于使用流程构建器的设置属性来启动或启动新流程,它检查调用命令是否是有效的操作系统命令,并检查与系统相关的命令是否无效。
start() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
start()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
start() method may throw an exception at the time of starting a new process.
start()方法在启动新进程时可能会引发异常。
- SecurityException: This exception may throw when its checkExec() method is not permitted to create the child process.SecurityException :如果不允许使用其checkExec()方法创建子进程,则可能引发此异常。
- IndexOutOfBoundsException: This exception may throw when the list of commands is blank.IndexOutOfBoundsException :当命令列表为空白时,可能会引发此异常。
- IOException:This exception may throw during input/output errors.IOException :此异常可能在输入/输出错误期间引发。
- NullPointerException: This exception may throw when null exists in the list of commands.NullPointerException :当命令列表中存在null时,可能引发此异常。
Syntax:
句法:
public Process start();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is Process, it returns newly created process to operate child processes.
该方法的返回类型为Process ,它返回新创建的流程来操作子流程。
Example:
例:
// Java program to demonstrate the example
// of Process start() method of ProcessBuilder
import java.io.*;
import java.util.*;
public class RedirectErrorStream {
public static void main(String[] args) throws Exception {
// Creating an object of List
List l = new LinkedList();
// By using add() method to add elements
l.add("TextPad.exe");
l.add("notepad.exe");
// Instantiating ProcessBuilder object
ProcessBuilder pr_bu = new ProcessBuilder(l);
// Start a process
Process p = pr_bu.start();
// Every process exit with a exit value
int val = p.waitFor();
System.out.println("Exit value = " + val);
}
}
Output
输出量
Exception in thread "main" java.io.IOException: CreateProcess: TextPad.exe notep
ad.exe error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.
(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at RedirectErrorStream.main(RedirectErrorStream.java:17)
翻译自: https://www.includehelp.com/java/processbuilder-start-method-with-example.aspx