/** * Executes the given command and returns a transcript of stderr and stdout * * @param command the command to execute * @return the transcript of stderr and stdout produced by the executed command * @throws ExecException in case the external program fails or returns an exit code other than 0. */ public static String exec(String command) throws ExecException { return exec(command, false); }
/** * Executes the given command and returns a transcript of stderr and stdout. * * @param command the command to execute * @param ignoreExitCodes if an exit code other than 0 should result in an exception being thrown * @return the transcript of stderr and stdout produced by the executed command * @throws ExecException in case the external program fails */ public static String exec(String command, boolean ignoreExitCodes) throws ExecException { StringBuffer logger = new StringBuffer(); try (Operation op = new Operation(() -> command, Duration.ofMinutes(5))) { Process p = Runtime.getRuntime().exec(command); Semaphore completionSynchronizer = new Semaphore(2); StreamEater errEater = StreamEater.eat(p.getErrorStream(), logger, completionSynchronizer); StreamEater outEater = StreamEater.eat(p.getInputStream(), logger, completionSynchronizer); doExec(ignoreExitCodes, logger, p); // Wait for the stream eaters to complete... completionSynchronizer.acquire(2); if (errEater.exHolder.get() != null) { throw new ExecException(errEater.exHolder.get(), logger.toString()); } if (outEater.exHolder.get() != null) { throw new ExecException(outEater.exHolder.get(), logger.toString()); } return logger.toString(); } catch (Exception e) { throw new ExecException(e, logger.toString()); } }
exec.exec(stmt); conn.commit();