Java ProcessBuilder command() 方法



描述

Java ProcessBuilder command(String... command) 方法設定此程序構建器的作業系統程式和引數。這是一種便捷方法,它將命令設定為一個字串列表,該列表包含與命令陣列相同的字串,並按相同的順序排列。不會檢查命令是否對應於有效的作業系統命令。

宣告

以下是 java.lang.ProcessBuilder.command() 方法的宣告

public ProcessBuilder command(String... command)

引數

command − 包含程式及其引數的字串陣列

返回值

此方法返回此程序構建器

異常

從 Process Builder 示例獲取記事本的程序列表

以下示例演示了 ProcessBuilder command() 方法的使用。在此程式中,我們建立了一個字串列表,並將 notepad.exe 和 text.txt 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。使用 command(list) 方法,我們將列表新增到 ProcessBuilder 中。然後,我們列印了底層作業系統命令名稱和其他詳細資訊。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"notepad.exe", "test.txt"};

      // 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());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

[notepad.exe, test.txt]

從 Process Builder 示例獲取計算器的程序列表

以下示例演示了 ProcessBuilder command() 方法的使用。在此程式中,我們建立了一個字串列表,並將 calc.exe 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。使用 command(list) 方法,我們將列表新增到 ProcessBuilder 中。然後,我們列印了底層作業系統命令名稱和其他詳細資訊。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"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 示例獲取瀏覽器的程序列表

以下示例演示了 ProcessBuilder command() 方法的使用。在此程式中,我們建立了一個字串列表,並將 explorer.exe 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。使用 command(list) 方法,我們將列表新增到 ProcessBuilder 中。然後,我們列印了底層作業系統命令名稱和其他詳細資訊。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"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
廣告