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 a new instance of ProcessBuilder:
        ProcessBuilder pb = new ProcessBuilder();
  1. Set the directory of execution to point to the directory of the shell script file:
         pb.directory(new File("/root"));
Note that the above path passed while creating the File object will depend on where you have place your script script.sh. In our case we had it placed in /root. You might have copied the script in /home/yourname and accordingly the File object will be created as new File("/home/yourname")
  1. Set an environment variable that would be used by the shell script:
        Map<String, String> environment = pb.environment();
        environment.put("MY_VARIABLE", "From your parent Java process");
  1. Set the command to be executed and also the arguments ...

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