JAVAJava中的lang.ProcessBuilder类

此类用于创建操作系统进程。每个ProcessBuilder实例管理一组流程属性。start()方法使用这些属性创建一个新的流程实例。可以从同一实例重复调用start()方法,以创建具有相同或相关属性的新子流程。

null
  • ProcessBuilder可用于帮助创建操作系统进程。
  • 在JDK 5.0之前,创建并执行进程的唯一方法是使用Runtime。exec()方法。
  • 它扩展了类对象。
  • 这个类是不同步的。

建造师:

  • ProcessBuilder(列表命令): 这将使用指定的操作系统程序和参数构造process builder。
  • ProcessBuilder(字符串…命令): 这将使用指定的操作系统程序和参数构造process builder。

方法:

1.List命令(): 此方法返回process builder的操作系统程序和参数。

Syntax: public List command().Returns: this process builder's program and its arguments.Exceptions: NullPointerException - if the argument is null.

JAVA

// Java code illustrating command() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating list of process
List<String> list = new ArrayList<String>();
list.add( "notepad.exe" );
// create the process
ProcessBuilder build = new ProcessBuilder(list);
// checking the command i list
System.out.println( "command: " + build.command());
}
}


输出

command: [notepad.exe]

2.ProcessBuilder命令(列表命令): 此方法设置process builder的操作系统程序和参数。

Syntax: public ProcessBuilder command(List command).Returns: NA.Exception: NullPointerException - if the argument is null.

JAVA

// Java code illustrating ProcessBuilder
// command(List<String> command)
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating list of process
List<String> list = new ArrayList<String>();
list.add( "notepad.exe" );
list.add( "xyz.txt" );
// create the process
ProcessBuilder build = new ProcessBuilder(list);
// checking the command in list
System.out.println( "command: " + build.command());
}
}


输出

command: [notepad.exe, xyz.txt]

3.ProcessBuilder目录(文件目录): 此方法设置process builder的工作目录。随后由object的start()方法启动的子进程将使用它作为其工作目录。参数可能为null——这意味着使用当前Java进程的工作目录,通常是由系统属性用户命名的目录。dir,作为子进程的工作目录。

Syntax: public ProcessBuilder directory(File directory).Returns: this process builder.Exception: NA.

JAVA

// Java code illustrating directory() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating list of process
List<String> list = new ArrayList<String>();
list.add( "notepad.exe" );
list.add( "abc.txt" );
// creating the process
ProcessBuilder build = new ProcessBuilder(list);
// setting the directory
build.directory( new File( "src" ));
// checking the directory, on which currently
// working on
System.out.println( "directory: "
+ build.directory());
}
}


输出

directory: src

4.映射环境(): 此方法返回process builder环境的字符串映射视图。无论何时创建process builder,环境都会初始化为当前流程环境的副本。随后由对象的start()方法启动的子进程将使用此映射作为其环境。

Syntax: public Map environment()Returns: this process builder's environmentException: SecurityException - if a security manager exists and its checkPermission method doesn't allow access to the process environment.

JAVA

// Java code illustrating environment() method
import java.io.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating the process
ProcessBuilder pb = new ProcessBuilder();
// map view of this process builder's environment
Map<String, String> envMap = pb.environment();
// checking map view of environment
for (Map.Entry<String, String> entry :
envMap.entrySet()) {
// checking key and value separately
System.out.println( "Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
}


输出:

Key = PATH, Value = /usr/bin:/bin:/usr/sbin:/sbinKey = JAVA_MAIN_CLASS_14267, Value = ProcessBuilderDemoKey = J2D_PIXMAPS, Value = sharedKey = SHELL, Value = /bin/bashKey = JAVA_MAIN_CLASS_11858, Value = org.netbeans.MainKey = USER, Value = abhishekvermaKey = TMPDIR, Value = /var/folders/9z/p63ysmfd797clc0468vvy4980000gn/T/Key = SSH_AUTH_SOCK, Value = /private/tmp/com.apple.launchd.uWvCfYQWBP/ListenersKey = XPC_FLAGS, Value = 0x0Key = LD_LIBRARY_PATH, Value = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/amd64:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/i386:Key = __CF_USER_TEXT_ENCODING, Value = 0x1F5:0x0:0x0Key = Apple_PubSub_Socket_Render, Value = /private/tmp/com.apple.launchd.weuNq4pAfF/RenderKey = LOGNAME, Value = abhishekvermaKey = LC_CTYPE, Value = UTF-8Key = XPC_SERVICE_NAME, Value = 0Key = PWD, Value = /Key = SHLVL, Value = 1Key = HOME, Value = /Users/abhishekvermaKey = _, Value = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java

了解上述输出: 输出是构建子流程的映射视图。以上输出因用户而异,完全取决于操作系统和用户。

5.布尔重定向错误流(): 此方法说明process builder是否合并标准错误和标准输出。如果此属性为true,则随后由对象的start()方法启动的子进程生成的任何错误输出都将与标准输出合并,以便可以使用该进程读取这两个输出。getInputStream()方法。这样可以更容易地将错误消息与相应的输出关联起来。初始值为false。

Syntax: public boolean redirectErrorStream().Returns: this process builder's redirectErrorStream property.Exception: NA.

JAVA

// Java code illustrating redirectErrorStream() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating list of commands
List list = new ArrayList();
list.add( "notepad.exe" );
list.add( "xyz.txt" );
// creating the process
ProcessBuilder build = new ProcessBuilder(list);
// checking if error stream is redirected
System.out.println(build.redirectErrorStream());
}
}


输出:

false

6.ProcessBuilder重定向错误流(布尔重定向错误流): 此方法设置process builder的redirectErrorStream属性。如果此属性为true,则随后由该对象的start()方法启动的子进程生成的任何错误输出都将与标准输出合并,以便可以使用该进程读取这两个输出。getInputStream()方法。这样可以更容易地将错误消息与相应的输出关联起来。初始值为false。

Syntax: public ProcessBuilder redirectErrorStream(boolean redirectErrorStream).Returns: this process builder.Exception: NA.

JAVA

// Java code illustrating redirectErrorStream(boolean
// redirectErrorStream) method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating list of commands
List list = new ArrayList();
list.add( "notepad.exe" );
list.add( "xyz.txt" );
// creating the process
ProcessBuilder build = new ProcessBuilder(list);
// redirecting error stream
build.redirectErrorStream( true );
// checking if error stream is redirected
System.out.println(build.redirectErrorStream());
}
}


输出:

true

进程开始(): 此方法使用process builder的属性启动新流程。新进程将调用command()给出的命令和参数,在directory()给出的工作目录中,在environment()给出的进程环境中。此方法检查该命令是否为有效的操作系统命令。哪些命令有效取决于系统,但该命令至少必须是非空字符串的非空列表。如果有一个安全管理器,它的checkExec方法将以该对象的命令数组的第一个组件作为参数来调用。这可能会导致抛出SecurityException。

Syntax: public Process start().Returns: a new Process object for managing the subprocess.Exception: NullPointerException - If an element of the command list is nullIndexOutOfBoundsException - If the command is an empty list (has size 0).SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess.IOException - If an I/O error occurs.

JAVA

// Java code illustrating start() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg) throws IOException
{
// creating list of commands
List<String> commands = new ArrayList<String>();
commands.add( "ls" ); // command
commands.add( "-l" ); // command
commands.add(
"/Users/abhishekverma" ); // command in Mac OS
// creating the process
ProcessBuilder pb = new ProcessBuilder(commands);
// starting the process
Process process = pb.start();
// for reading the output from stream
BufferedReader stdInput
= new BufferedReader( new InputStreamReader(
process.getInputStream()));
String s = null ;
while ((s = stdInput.readLine()) != null ) {
System.out.println(s);
}
}
}


输出:

total 0drwxr-xr-x  10 abhishekverma  staff   340 Jun 20 02:24 AndroidStudioProjectsdrwx------@ 22 abhishekverma  staff   748 Jun 20 03:00 Desktopdrwx------@  7 abhishekverma  staff   238 Apr 29 22:03 Documentsdrwx------+ 27 abhishekverma  staff   918 Jun 20 03:01 Downloadsdrwx------@ 65 abhishekverma  staff  2210 Jun 18 20:48 Librarydrwx------+  3 abhishekverma  staff   102 Mar 28 13:08 Moviesdrwx------+  4 abhishekverma  staff   136 Apr  8 04:51 Musicdrwxr-xr-x   4 abhishekverma  staff   136 Jun 19 15:01 NetBeansProjectsdrwx------+  5 abhishekverma  staff   170 Apr 10 09:46 Picturesdrwxr-xr-x+  6 abhishekverma  staff   204 Jun 18 20:45 Public-rw-r--r--   1 abhishekverma  staff     0 Apr 15 19:23 newreactjs.jsx

ProcessBuilder Inheritario(): 将子流程标准I/O的源和目标设置为与当前Java流程的源和目标相同。

Syntax: public ProcessBuilder inheritIO().Returns: this process builder.Exception: NA.

JAVA

// Java code illustrating inheritIO() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
public static void main(String[] arg)
throws IOException, InterruptedException
{
ProcessBuilder pb = new ProcessBuilder(
"echo" , "Hello GeeksforGeeks"
+ "This is ProcessBuilder Example" );
pb.inheritIO();
Process process = pb.start();
process.waitFor();
}
}


输出:

Hello GeeksforGeeksThis is ProcessBuilder Example

本文由 阿布舍克·维马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享