@Test public void execProcessWithWrongCommand() { List<String> commandLine = Lists.newArrayList("not-existing-command", "-wrongArgument"); int result = ProcessUtil.execProcess(".", commandLine); assertThat(result, is(-1)); }
@SuppressWarnings("squid:S1166") public static int execProcess(String directory, List<String> cmd) { File procDirectory = new File(directory); LOGGER.debug("Starting process [dir:{}]: {}", procDirectory.getAbsolutePath(), cmd); ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.directory(procDirectory); String stdOut = null; String stdError = null; int result = -1; long start = System.currentTimeMillis(); try { Process process = processBuilder.start(); result = process.waitFor(); stdOut = CharStreams.toString(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); stdError = CharStreams.toString(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset())); } catch (IOException e) { LOGGER.error("Error starting the postprocessing command '{}': {}", cmd.get(0), e.getMessage()); } catch (InterruptedException e) { LOGGER.error("Error postprocessing '{}': {}", cmd.get(0), e.getMessage()); Thread.currentThread().interrupt(); } long end = System.currentTimeMillis(); String successString = (result == 0) ? "SUCCESS: " : "FAILURE: "; LOGGER.info("{} executed command '{}' in {} ms", successString, cmd, (end - start)); if (stdOut != null && !stdOut.isEmpty()) { LOGGER.info("Stdout: {}", stdOut); } if (stdError != null && !stdError.isEmpty()) { LOGGER.info("Stderr: {}", stdError); } return result; }
ProcessUtil { @SuppressWarnings("squid:S1166") public static int execProcess(String directory, List<String> cmd) { File procDirectory = new File(directory); LOGGER.debug("Starting process [dir:{}]: {}", procDirectory.getAbsolutePath(), cmd); ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.directory(procDirectory); String stdOut = null; String stdError = null; int result = -1; long start = System.currentTimeMillis(); try { Process process = processBuilder.start(); result = process.waitFor(); stdOut = CharStreams.toString(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); stdError = CharStreams.toString(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset())); } catch (IOException e) { LOGGER.error("Error starting the postprocessing command '{}': {}", cmd.get(0), e.getMessage()); } catch (InterruptedException e) { LOGGER.error("Error postprocessing '{}': {}", cmd.get(0), e.getMessage()); Thread.currentThread().interrupt(); } long end = System.currentTimeMillis(); String successString = (result == 0) ? "SUCCESS: " : "FAILURE: "; LOGGER.info("{} executed command '{}' in {} ms", successString, cmd, (end - start)); if (stdOut != null && !stdOut.isEmpty()) { LOGGER.info("Stdout: {}", stdOut); } if (stdError != null && !stdError.isEmpty()) { LOGGER.info("Stderr: {}", stdError); } return result; } }
ProcessUtil { @SuppressWarnings("squid:S1166") public static int execProcess(String directory, List<String> cmd) { File procDirectory = new File(directory); LOGGER.debug("Starting process [dir:{}]: {}", procDirectory.getAbsolutePath(), cmd); ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.directory(procDirectory); String stdOut = null; String stdError = null; int result = -1; long start = System.currentTimeMillis(); try { Process process = processBuilder.start(); result = process.waitFor(); stdOut = CharStreams.toString(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); stdError = CharStreams.toString(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset())); } catch (IOException e) { LOGGER.error("Error starting the postprocessing command '{}': {}", cmd.get(0), e.getMessage()); } catch (InterruptedException e) { LOGGER.error("Error postprocessing '{}': {}", cmd.get(0), e.getMessage()); Thread.currentThread().interrupt(); } long end = System.currentTimeMillis(); String successString = (result == 0) ? "SUCCESS: " : "FAILURE: "; LOGGER.info("{} executed command '{}' in {} ms", successString, cmd, (end - start)); if (stdOut != null && !stdOut.isEmpty()) { LOGGER.info("Stdout: {}", stdOut); } if (stdError != null && !stdError.isEmpty()) { LOGGER.info("Stderr: {}", stdError); } return result; } private  ProcessUtil(); }
ProcessUtil { @SuppressWarnings("squid:S1166") public static int execProcess(String directory, List<String> cmd) { File procDirectory = new File(directory); LOGGER.debug("Starting process [dir:{}]: {}", procDirectory.getAbsolutePath(), cmd); ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.directory(procDirectory); String stdOut = null; String stdError = null; int result = -1; long start = System.currentTimeMillis(); try { Process process = processBuilder.start(); result = process.waitFor(); stdOut = CharStreams.toString(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); stdError = CharStreams.toString(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset())); } catch (IOException e) { LOGGER.error("Error starting the postprocessing command '{}': {}", cmd.get(0), e.getMessage()); } catch (InterruptedException e) { LOGGER.error("Error postprocessing '{}': {}", cmd.get(0), e.getMessage()); Thread.currentThread().interrupt(); } long end = System.currentTimeMillis(); String successString = (result == 0) ? "SUCCESS: " : "FAILURE: "; LOGGER.info("{} executed command '{}' in {} ms", successString, cmd, (end - start)); if (stdOut != null && !stdOut.isEmpty()) { LOGGER.info("Stdout: {}", stdOut); } if (stdError != null && !stdError.isEmpty()) { LOGGER.info("Stderr: {}", stdError); } return result; } private  ProcessUtil(); @SuppressWarnings("squid:S3457") // wants parameters in the log statement; would not be helpful here. static synchronized void finish(); static void execProcessInBackground(final String directory, final List<String> cmd); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static int execProcess(String directory, List<String> cmd); }
ProcessUtil { @SuppressWarnings("squid:S1166") public static int execProcess(String directory, List<String> cmd) { File procDirectory = new File(directory); LOGGER.debug("Starting process [dir:{}]: {}", procDirectory.getAbsolutePath(), cmd); ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.directory(procDirectory); String stdOut = null; String stdError = null; int result = -1; long start = System.currentTimeMillis(); try { Process process = processBuilder.start(); result = process.waitFor(); stdOut = CharStreams.toString(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); stdError = CharStreams.toString(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset())); } catch (IOException e) { LOGGER.error("Error starting the postprocessing command '{}': {}", cmd.get(0), e.getMessage()); } catch (InterruptedException e) { LOGGER.error("Error postprocessing '{}': {}", cmd.get(0), e.getMessage()); Thread.currentThread().interrupt(); } long end = System.currentTimeMillis(); String successString = (result == 0) ? "SUCCESS: " : "FAILURE: "; LOGGER.info("{} executed command '{}' in {} ms", successString, cmd, (end - start)); if (stdOut != null && !stdOut.isEmpty()) { LOGGER.info("Stdout: {}", stdOut); } if (stdError != null && !stdError.isEmpty()) { LOGGER.info("Stderr: {}", stdError); } return result; } private  ProcessUtil(); @SuppressWarnings("squid:S3457") // wants parameters in the log statement; would not be helpful here. static synchronized void finish(); static void execProcessInBackground(final String directory, final List<String> cmd); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static int execProcess(String directory, List<String> cmd);  }