Java – How to run Unix shell script from Java code


It is quite simple to run a Unix command from Java.


But is it possible to run a Unix shell script from Java code? If yes, would it be a good practice to run a shell script from within Java code?

Best Solution

You should really look at Process Builder. It is really built for this kind of thing.

ProcessBuilder pb = new ProcessBuilder("", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.put("VAR2", env.get("VAR1") + "suffix"); File("myDir"));
 Process p = pb.start();