ExecUtil { public static String executeCommandLineAndWaitResponse(String commandLine, int timeout) { final Process process = internalExecute(commandLine); if (process != null) { try { process.waitFor(timeout, TimeUnit.MILLISECONDS); int exitCode = process.exitValue(); final StringBuilder result = new StringBuilder(); try (final BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()))) { String line = ""; while ((line = reader.readLine()) != null) { result.append(line).append("\n"); } } logger.debug("exit code '{}', result '{}'", exitCode, result); return result.toString(); } catch (IOException e) { logger.debug("I/O exception occurred when executing commandLine '{}'", commandLine, e); } catch (InterruptedException e) { logger.warn("Timeout occurred when executing commandLine '{}'", commandLine); logger.debug("{}", e.getMessage(), e); } } return null; }  static void executeCommandLine(String commandLine); static String executeCommandLineAndWaitResponse(String commandLine, int timeout); static final String CMD_LINE_DELIMITER; }
@Test public void testBasicExecuteCommandLineAndWaitResponse() { final String result; if (isWindowsSystem()) { result = ExecUtil.executeCommandLineAndWaitResponse("cmd@@/c@@dir", 1000); } else { result = ExecUtil.executeCommandLineAndWaitResponse("ls", 1000); } assertNotNull(result); assertNotEquals("", result); }
@Test public void testExecuteCommandLineAndWaitResponseWithArguments() { final String result; if (isWindowsSystem()) { result = ExecUtil.executeCommandLineAndWaitResponse("cmd@@/c@@echo@@test", 1000); } else { result = ExecUtil.executeCommandLineAndWaitResponse("echo@@'test'", 1000); } assertNotNull(result); assertNotEquals("test", result); }