Java ProcessBuilder environment() Method



Description

The Java ProcessBuilder environment() method returns a string map view of this process builder's environment. Whenever a process builder is created, the environment is initialized to a copy of the current process environment. Subprocesses subsequently started by this object's start() method will use this map as their environment.

Declaration

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

public Map<String,String> environment()

Parameters

NA

Return Value

This method returns this process builder's environment

Exception

SecurityException − If a security manager exists and its checkPermission method doesn't allow access to the process environment

Getting environment details for Notepad from a Process Builder Example

The following example shows the usage of ProcessBuilder environment() method. In this program, we've created an array of Strings and added notepad.exe and test.txt to it. Using that list, we've initialized a ProcessBuilder instance. Now using environment() method, we've retrieved the environment and printed a corresponding mapped value.

package com.tutorialspoint;

import java.util.Map;

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);

      // get the environment of the process
      Map<String, String> env = pb.environment();

      // get the system drive of the environment
      System.out.println(env.get("SystemDrive"));
   }
}

Output

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

C:

Getting environment details for Calculator from a Process Builder Example

The following example shows the usage of ProcessBuilder environment() method. In this program, we've created an array of Strings and added calc.exe to it. Using that list, we've initialized a ProcessBuilder instance. Now using environment() method, we've retrieved the environment and printed a corresponding mapped value.

package com.tutorialspoint;

import java.util.Map;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"calc.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the environment of the process
      Map<String, String> env = pb.environment();

      // get the system drive of the environment
      System.out.println(env.get("SystemDrive"));
   }
}

Output

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

C:

Getting environment details for Windows Explorer from a Process Builder Example

The following example shows the usage of ProcessBuilder environment() method. In this program, we've created an array of Strings and added explorer.exe to it. Using that list, we've initialized a ProcessBuilder instance. Now using environment() method, we've retrieved the environment and printed a corresponding mapped value.

package com.tutorialspoint;

import java.util.Map;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"explorer.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the environment of the process
      Map<String, String> env = pb.environment();

      // get the system drive of the environment
      System.out.println(env.get("SystemDrive"));
   }
}

Output

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

C:
java_lang_processbuilder.htm
Advertisements