Java.lang.ProcessBuilder.command() Method



Description

The java.lang.ProcessBuilder.command(List<String> command) method sets this process builder's operating system program and arguments. This method does not make a copy of the command list. Subsequent updates to the list will be reflected in the state of the process builder. It is not checked whether command corresponds to a valid operating system command.

Declaration

Following is the declaration for java.lang.ProcessBuilder.command() method

public ProcessBuilder command(List<String> command)

Parameters

command − The list containing the program and its arguments

Return Value

This method returns this process builder

Exception

NullPointerException − If the argument is null

Example

The following example shows the usage of lang.ProcessBuilder.command() method.

package com.tutorialspoint;

import java.io.IOException;
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<String>();
      list.add("notepad.exe");

      // create a new list that contains a file to open with notepad
      List<String> list2 = new ArrayList<String>();
      list.add("text.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());
   }
}

Let us compile and run the above program, this will produce the following result −

[notepad.exe, text.txt]
java_lang_processbuilder.htm
Advertisements