ProcessBuilder environment() in Java

environment(): This method is available in the java.lang.ProcessBuilder class of Java.

Syntax:

Map<String, String> java.lang.ProcessBuilder.environment()

This 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.

Parameters: NA

Returns: this process builder's environment.

Throws:

1. SecurityException - if a security manager exists and its checkPermissionmethod doesn't allow access to the process environment.

Approach

Java

package com.ProcessBuilder;

public class ProcessBuilderenvironment {
    public static void main(String[] args) {

        ProcessBuilder processBuilder =
new ProcessBuilder("notepad.exe");

        System.out.println(processBuilder.environment());
    }
}

Output:

{USERDOMAIN_ROAMINGPROFILE=DESKTOP-OK73JVQ, LOCALAPPDATA=C:\Users\Admin\AppData\Local, PROCESSOR_LEVEL=6, USERDOMAIN=DESKTOP-OK73JVQ, FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer, LOGONSERVER=\\DESKTOP-OK73JVQ, JAVA_HOME=D:\tools\jdk-11.0.11_windows-x64_bin\jdk-11.0.11, SESSIONNAME=Console, ALLUSERSPROFILE=C:\ProgramData, LEADSINTERVAL=0 0/2 * * * ?, PROCESSOR_ARCHITECTURE=AMD64, PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules, SystemDrive=C:, MAVEN_HOME=D:\tools\apache-maven-3.8.4, OneDrive=C:\Users\Admin\OneDrive, APPDATA=C:\Users\Admin\AppData\Roaming, USERNAME=Admin, ProgramFiles(x86)=C:\Program Files (x86), CommonProgramFiles=C:\Program Files\Common Files, Path=D:/Java/sts-4.9.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.1.v20201027-0507/jre/bin/server;D:/Java/sts-4.9.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.1.v20201027-0507/jre/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;%MAVEN_HOME%\bin;D:\tools\apache-maven-3.8.4\bin;D:\tools\jdk-11.0.11_windows-x64_bin\jdk-11.0.11\bin;C:\Program Files\TortoiseSVN\bin;C:\kafka_2.12-3.2.3\bin\windows;C:\Users\Admin\AppData\Local\Programs\Python\Python39\Scripts\;C:\Users\Admin\AppData\Local\Programs\Python\Python39\;C:\Users\Admin\AppData\Local\Microsoft\WindowsApps;C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin;JAVA_HOME;C:\Users\Admin\Downloads\jdk-11.0.11_windows-x64_bin\jdk-11.0.11\bin;D:\tools\flutter\bin;;D:\Java\sts-4.9.0.RELEASE;, FPS_BROWSER_USER_PROFILE_STRING=Default, PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, DriverData=C:\Windows\System32\Drivers\DriverData, OS=Windows_NT, OneDriveConsumer=C:\Users\Admin\OneDrive, COMPUTERNAME=DESKTOP-OK73JVQ, PROCESSOR_REVISION=3d04, CommonProgramW6432=C:\Program Files\Common Files, FB_ACCESS_KEY=EAAMJf6VHIegBAKJ562ygjdzRI3JGy8UOcQ4gJPRpEsJiZCNFfWDBJ9H5E9qZCP62GNypxbpmq3nEcVNiqVylxdmpkKBGN7glB2UkTaOSVwnKYVr6Mnnwj8BpF2w1hlo10qvqoXxONjo7ieF7PhmzLRsB5FQCeDXgt6NUE8hyLoSIG9UxsvKZBtVVZAxQM7heajqYXBw4fAZDZD, ComSpec=C:\WINDOWS\system32\cmd.exe, ProgramData=C:\ProgramData, ProgramW6432=C:\Program Files, REPORT_PATH=D:/report, HOMEPATH=\Users\Admin, SystemRoot=C:\WINDOWS, TEMP=C:\Users\Admin~1\AppData\Local\Temp, HOMEDRIVE=C:, PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 61 Stepping 4, GenuineIntel, USERPROFILE=C:\Users\Admin, TMP=C:\Users\JITEND~1\AppData\Local\Temp, CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files, ProgramFiles=C:\Program Files, PUBLIC=C:\Users\Public, NUMBER_OF_PROCESSORS=4, windir=C:\WINDOWS, =::=::\}


Some methods of ProcessBuilder class

ProcessBuilder.ProcessBuilder(String...)This method constructs a process builder with the specified operating system program and arguments.

ProcessBuilder.ProcessBuilder(List<String>)This method constructs a process builder with the specified operating system program and arguments.

command()This method returns this process builder's operating system program and arguments.

command(List<String>)This method sets this process builder's operating system program and arguments.

command(String...)This method sets this process builder's operating system program and arguments. 

directory()This method returns this process builder's working directory.

directory(File)This method sets this process builder's working directory.

environment()This method returns a string map view of this process builder's environment.

inheritIO()This method sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

redirectError()This method returns this process builder's standard error destination.

redirectError(File)This method sets this process builder's standard error destination to a file.

redirectError(Redirect)This method sets this process builder's standard error destination.

redirectErrorStream()This method tells whether this process builder merges standard error and standard output.

redirectErrorStream(boolean)This method sets this process builder's redirectErrorStream property.

redirectInput()This method returns this process builder's standard input source.

redirectInput(File)This method sets this process builder's standard input source to a file.

redirectInput(Redirect)This method sets this process builder's standard input source.

redirectOutput()This method returns this process builder's standard output destination.

redirectOutput(File)This method sets this process builder's standard output destination to a file.

redirectOutput(Redirect)This method sets this process builder's standard output destination.

start()This method starts a new process using the attributes of this process builder.

ProcessBuilder.startPipeline(List<ProcessBuilder>)his method starts a Process for each ProcessBuilder, creating a pipeline of processes linked by their standard output and standard input streams.

No comments:

Post a Comment