import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Jceks { public static void main(String[] args) { System.out.println("Hello jceks..."); System.out.println(Arrays.toString(args)); try { run(args); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void run(String[] command) throws Exception { System.out.println(String.join(" ", command)); int exitCode = 0; StringBuilder output = new StringBuilder(); ProcessBuilder pb = new ProcessBuilder(command); pb.redirectErrorStream(true); Process p = pb.start(); exitCode = p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } System.out.println(output); } }
Tuesday, June 27, 2017
Run a command with ProcessBuilder in Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment