Java.lang.ProcessBuilder.start() Method

Advertisements


Description

The java.lang.ProcessBuilder.start() method starts a new process using the attributes of this process builder. The new process will invoke the command and arguments given by command(), in a working directory as given by directory(), with a process environment as given by environment(). This method checks that the command is a valid operating system command. Which commands are valid is system-dependent, but at the very least the command must be a non-empty list of non-null strings.

If there is a security manager, its checkExec method is called with the first component of this object's command array as its argument. This may result in a SecurityException being thrown.

Declaration

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

public Process start()

Parameters

  • NA

Return Value

This method returns a new Process object for managing the subprocess

Exception

  • NullPointerException -- If an element of the command list is null

  • IndexOutOfBoundsException -- If the command is an empty list (has size 0)

  • SecurityException -- If a security manager exists and its checkExec method doesn't allow creation of the subprocess

  • IOException -- If an I/O error occurs

Example

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

package com.tutorialspoint;

import java.io.IOException;

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);
      try {
         // start the subprocess
         System.out.println("Starting the process..");
         pb.start();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

Starting the process..



Advertisements
Advertisements