Java Code Examples for org.zeroturnaround.exec.ProcessExecutor#execute()
The following examples show how to use
org.zeroturnaround.exec.ProcessExecutor#execute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultCommandRunner.java From carnotzet with Apache License 2.0 | 6 votes |
public String runCommandAndCaptureOutput(File directoryForRunning, String... command) { log.debug("Running command [{}]", Joiner.on(" ").join(command)); ProcessExecutor pe = new ProcessExecutor() .command(command) .redirectErrorStream(true) .directory(directoryForRunning) .readOutput(true); try { ProcessResult processResult = pe.execute(); String output = processResult.outputUTF8().trim(); if (processResult.getExitValue() != 0) { throw new RuntimeException("External command [" + Joiner.on(" ").join(command) + "] exited with [" + processResult.getExitValue() + "], output: " + output); } return output; } catch (InterruptedException | IOException | TimeoutException e) { throw new RuntimeException(e); } }
Example 2
Source File: ErlangShell.java From embedded-rabbitmq with Apache License 2.0 | 5 votes |
/** * @return a String representing the Erlang version, such as {@code "18.2.1"} * @throws ErlangShellException if the Erlang command can't be executed or if it exits unexpectedly. */ public String getErlangVersion() throws ErlangShellException { String erlangShell = UNIX_ERL_COMMAND; Logger processOutputLogger = LoggerFactory.getLogger( String.format(LOGGER_TEMPLATE, this.getClass().getName(), erlangShell)); Slf4jStream stream = Slf4jStream.of(processOutputLogger); final ProcessExecutor processExecutor = config.getProcessExecutorFactory().createInstance() .command(erlangShell, "-noshell", "-eval", "erlang:display(erlang:system_info(otp_release)), halt().") .timeout(config.getErlangCheckTimeoutInMillis(), TimeUnit.MILLISECONDS) .redirectError(stream.as(Level.WARN)) .destroyOnExit() .readOutput(true); try { ProcessResult processResult = processExecutor.execute(); int exitValue = processResult.getExitValue(); if (exitValue == 0) { return processResult.outputUTF8().trim().replaceAll("[\"\\\\n]", ""); // "18.2.1\n" -> "18.2.1" } else { throw new ErlangShellException("Erlang exited with status " + exitValue); } } catch (IOException | InterruptedException | TimeoutException e) { throw new ErlangShellException("Exception executing Erlang shell command", e); } }
Example 3
Source File: ProcessExecutorInputStreamTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Test public void testWithInputAndRedirectOutput() throws Exception { String str = "Tere Minu Uus vihik"; ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ProcessExecutor exec = new ProcessExecutor("java", "-cp", "target/test-classes", PrintInputToOutput.class.getName()); exec.redirectInput(bais).redirectOutput(baos); exec.execute(); Assert.assertEquals(str, baos.toString()); }
Example 4
Source File: ProcessExecutorMainTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Test public void testJavaVersionOutputTwice() throws Exception { ProcessExecutor executor = new ProcessExecutor().command("java", "-version").readOutput(true); ProcessResult result = executor.execute(); String str = result.outputUTF8(); Assert.assertFalse(StringUtils.isEmpty(str)); Assert.assertEquals(str, executor.execute().outputUTF8()); }
Example 5
Source File: ProjectCommandExecutor.java From spring-cloud-release-tools with Apache License 2.0 | 4 votes |
ProcessResult doExecute(ProcessExecutor processExecutor) throws IOException, InterruptedException, TimeoutException { return processExecutor.execute(); }