java.lang.ProcessBuilder command(List<String> command)

Description

This java example source code demonstrates the use of command(List<String> command) method of ProcessBuilder class.

Some takeaways on this method:

  • This method sets this process builder’s operating system program and argument.
  • 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.
  • NullPointerException can be thrown by this method if the argument is null.

Method Syntax

public ProcessBuilder command(List<String> command)

Method Argument

Data Type Parameter Description
List<String> command the list containing the program and its arguments

Method Returns

The command(List<String> command) method returns this ProcessBuilder.

Compatibility

Requires Java 1.5 and up

Java ProcessBuilder command(List<String> command) Example

Below is a java code demonstrates the use of command(List<String> command) method of ProcessBuilder class.

package com.javatutorialhq.java.examples;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 * This example source code demonstrates the use of  
 * command(List command) method of Double class
 */

public class ProcessBuilderCommandList {

	public static void main(String[] args) {

		/*
		 * This is a concrete example 
		 * that launch the command prompt 
		 * and execute directory listing
		 */

		List alist = new ArrayList<>();

		// add the list of commands to the list
		alist.add("cmd.exe");
		alist.add("/C");
		alist.add("start;dir /w;");

		// initialize the processbuilder
		ProcessBuilder builder = new ProcessBuilder();
		builder.command(alist);
		try {
			// start the process
			builder.start();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Sample Output

Below is the sample output when you run the above example.

ProcessBuilder command(List command) method example