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 to do it...

  1. Create an instance of ProcessBuilder:
        ProcessBuilder pb = new ProcessBuilder();
  1. Set the command to printenv and the output and error streams to the same as that of the current Java process:
        pb.command("printenv").inheritIO();
  1. Provide the environment variables, COOKBOOK_VAR1 with the value, First variableCOOKBOOK_VAR2 with the value, Second variable, and COOKBOOK_VAR3 with the value, Third variable:
        Map<String, String> environment = pb.environment();
        environment.put("COOKBOOK_VAR1", "First variable");
        environment.put("COOKBOOK_VAR2", "Second variable");
        environment.put("COOKBOOK_VAR3", "Third variable");
  1. Start the process and wait for it to complete:
        Process p = pb.start();
        int exitValue = p.waitFor();

The complete ...

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