

Map environment = processBuilder.environment() Įnvironment.forEach((key, value) -> (key + value)) In this next example, we're going to see how to modify the working environment.īut before we do that let's begin by taking a look at the kind of information we can find in the default environment: ProcessBuilder processBuilder = new ProcessBuilder() For example, if we have multiple threads accessing a ProcessBuilder instance concurrently then the synchronization must be managed externally. We should note that this class is NOT synchronized. Last but not least, to start a new process with what we've configured, we simply call start(). When we want to redirect the process builder’s standard input, output and error destination to a file, we have these three similar redirect methods at our disposal. redirectInput(File file), redirectOutput(File file), redirectError(File file).If we want to specify that the source and destination for our subprocess standard I/O should be the same as that of the current Java process, we can use the inheritIO method. It returns us a copy of the current process environment using System.getenv() but as a Map. If we want to get the current environment variables, we can simply call the environment method. By default, the current working directory is set to the value returned by the user.dir system property. We can override the default working directory of the current process by calling the directory method and passing a File object. To create a new process builder with the specified operating system program and arguments, we can use this convenient constructor. This will help us when we dive into some real examples later on:


In this section, we're going to take a step back and briefly look at the most important methods in the ProcessBuilder class.
