Java ProcessBuilder command() 方法



描述

Java ProcessBuilder command(List<String> command) 方法设置此进程构建器的操作系统程序及其参数。此方法不会复制命令列表。后续对列表的更新将反映在进程构建器的状态中。不会检查命令是否对应于有效的操作系统命令。

声明

以下是java.lang.ProcessBuilder.command() 方法的声明

public ProcessBuilder command(List<String> command)

参数

command − 包含程序及其参数的列表

返回值

此方法返回此进程构建器

异常

NullPointerException − 如果参数为 null

从 Process Builder 示例中获取记事本进程列表

以下示例演示了 ProcessBuilder command() 方法的用法。在这个程序中,我们创建了一个字符串列表并将 notepad.exe 添加到其中。使用该列表,我们初始化了一个 ProcessBuilder 实例。使用 command(list) 方法,我们将列表添加到 ProcessBuilder。然后我们打印底层操作系统的命令名称和其他详细信息。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      List<String> list = new ArrayList<>();
      list.add("notepad.exe");

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // add a new argument to the list
      list.add("text.txt");
	  
      // set the command list
      pb.command(list);

      // print the new command list
      System.out.println(pb.command());
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

[notepad.exe, text.txt]

从 Process Builder 示例中获取计算器进程列表

以下示例演示了 ProcessBuilder command() 方法的用法。在这个程序中,我们创建了一个字符串列表并将 calc.exe 添加到其中。使用该列表,我们初始化了一个 ProcessBuilder 实例。使用 command(list) 方法,我们将列表添加到 ProcessBuilder。然后我们打印底层操作系统的命令名称和其他详细信息。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      List<String> list = new ArrayList<>();
      list.add("calc.exe");

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // set the command list
      pb.command(list);

      // print the new command list
      System.out.println(pb.command());
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

[calc.exe]

从 Process Builder 示例中获取 Windows 资源管理器进程列表

以下示例演示了 ProcessBuilder command() 方法的用法。在这个程序中,我们创建了一个字符串列表并将 explorer.exe 添加到其中。使用该列表,我们初始化了一个 ProcessBuilder 实例。使用 command(list) 方法,我们将列表添加到 ProcessBuilder。然后我们打印底层操作系统的命令名称和其他详细信息。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      List<String> list = new ArrayList<>();
      list.add("explorer.exe");

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);
	  
      // set the command list
      pb.command(list);

      // print the new command list
      System.out.println(pb.command());
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

[explorer.exe]
java_lang_processbuilder.htm
广告
© . All rights reserved.