- Create a new instance of ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder();
- Set the directory of execution to point to the directory of the shell script file:
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").
- 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");
- Set the command to be executed and also the arguments ...