Java ProcessBuilder command() 方法



描述

Java ProcessBuilder command() 方法返回此程序構建器的作業系統程式及其引數。返回的列表不是副本。對列表的後續更新將反映在此程序構建器的狀態中。

宣告

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

public List<String> command()

引數

返回值

此方法返回此程序構建器的程式及其引數

異常

從 Process Builder 示例獲取記事本的程序詳細資訊

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

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);
      
      // get the command list
      System.out.println(pb.command());
   }
}

輸出

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

[notepad.exe]

從 Process Builder 示例獲取計算器的程序詳細資訊

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

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);
      
      // get the command list
      System.out.println(pb.command());
   }
}

輸出

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

[calc.exe]

從 Process Builder 示例獲取 Windows 資源管理器的程序詳細資訊

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

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);
      
      // get the command list
      System.out.println(pb.command());
   }
}

輸出

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

[notepad.exe]
java_lang_processbuilder.htm
廣告