O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

When you invoke the environment() method on the instance of ProcessBuilder, it copies the environment variables of the current process, populates them in an instance of HashMap, and returns it to the caller code.

All the work of loading the environment variables is done by a package private final class, ProcessEnvironment, which actually extends HashMap.

We then make use of this map to populate our own environment variables, but we need not set the map back to ProcessBuilder because we will have a reference to the map object and not a copy. Any changes made to the map object will reflect in the actual map object held by the ProcessBuilder instance. 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required