- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java ProcessBuilder command() Method
Description
The Java 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
Getting list of process for Notepad from a Process Builder Example
The following example shows the usage of ProcessBuilder command() method. In this program, we've created a list of Strings and added notepad.exe to it. Using that list, we've initialized a ProcessBuilder instance. Using command(list) method, we've added a list to the ProcessBuilder. Then we've printed the underlying operating system command name and other details.
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());
}
}
Output
Let us compile and run the above program, this will produce the following result −
[notepad.exe, text.txt]
Getting list of process for Calculator from a Process Builder Example
The following example shows the usage of ProcessBuilder command() method. In this program, we've created a list of Strings and added calc.exe to it. Using that list, we've initialized a ProcessBuilder instance. Using command(list) method, we've added a list to the ProcessBuilder. Then we've printed the underlying operating system command name and other details.
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());
}
}
Output
Let us compile and run the above program, this will produce the following result −
[calc.exe]
Getting list of process for Windows Explorer from a Process Builder Example
The following example shows the usage of ProcessBuilder command() method. In this program, we've created a list of Strings and added explorer.exe to it. Using that list, we've initialized a ProcessBuilder instance. Using command(list) method, we've added a list to the ProcessBuilder. Then we've printed the underlying operating system command name and other details.
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());
}
}
Output
Let us compile and run the above program, this will produce the following result −
[explorer.exe]