From f9ba471da18307ca98c3b8d617275b089961b7b8 Mon Sep 17 00:00:00 2001 From: Andrew Fiddian-Green Date: Sat, 16 Jan 2021 21:20:09 +0000 Subject: [PATCH] [executeCommandLine] should return STDERR if STDOUT is empty (#2114) Signed-off-by: Andrew Fiddian-Green --- .../openhab/core/io/net/exec/ExecUtil.java | 31 +++---------------- .../core/io/net/exec/ExecUtilTest.java | 13 ++++++++ 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/exec/ExecUtil.java b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/exec/ExecUtil.java index afc0b51b62f..0b568e0e094 100644 --- a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/exec/ExecUtil.java +++ b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/exec/ExecUtil.java @@ -78,9 +78,8 @@ public static void executeCommandLine(String... commandLine) { Process processTemp = null; Future outputFuture = null; - Future errorFuture = null; cleanup: try { - Process process = processTemp = new ProcessBuilder(commandLine).start(); + Process process = processTemp = new ProcessBuilder(commandLine).redirectErrorStream(true).start(); outputFuture = executor.submit(() -> { try (InputStream inputStream = process.getInputStream(); @@ -91,32 +90,13 @@ public static void executeCommandLine(String... commandLine) { } }); - errorFuture = executor.submit(() -> { - try (InputStream inputStream = process.getErrorStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - StringWriter output = new StringWriter(); - reader.transferTo(output); - return output.toString(); - } - }); - int exitCode; if (timeout == null) { - exitCode = process.waitFor(); - } else if (process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS)) { - exitCode = process.exitValue(); - } else { + process.waitFor(); + } else if (!process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.warn("Timeout occurred when executing commandLine '{}'", Arrays.toString(commandLine)); break cleanup; } - if (exitCode == 0) { - return outputFuture.get(); - } else { - if (logger.isDebugEnabled()) { - logger.debug("exit code '{}', result '{}', errors '{}'", exitCode, outputFuture.get(), - errorFuture.get()); - } - return errorFuture.get(); - } + return outputFuture.get(); } catch (ExecutionException e) { if (logger.isDebugEnabled()) { logger.warn("Error occurred when executing commandLine '{}'", Arrays.toString(commandLine), @@ -139,9 +119,6 @@ public static void executeCommandLine(String... commandLine) { if (outputFuture != null) { outputFuture.cancel(true); } - if (errorFuture != null) { - errorFuture.cancel(true); - } return null; } } diff --git a/bundles/org.openhab.core.io.net/src/test/java/org/openhab/core/io/net/exec/ExecUtilTest.java b/bundles/org.openhab.core.io.net/src/test/java/org/openhab/core/io/net/exec/ExecUtilTest.java index 8c5373c035f..93fe5e474d7 100644 --- a/bundles/org.openhab.core.io.net/src/test/java/org/openhab/core/io/net/exec/ExecUtilTest.java +++ b/bundles/org.openhab.core.io.net/src/test/java/org/openhab/core/io/net/exec/ExecUtilTest.java @@ -62,4 +62,17 @@ private boolean isWindowsSystem() { String osName = System.getProperty("os.name").toLowerCase(); return osName.indexOf("windows") >= 0; } + + @Test + public void testExecuteCommandLineAndWaitStdErrRedirection() { + final String result; + if (isWindowsSystem()) { + result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "dir", "xxx.xxx", + "1>", "nul"); + } else { + result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "ls", "xxx.xxx"); + } + assertNotNull(result); + assertNotEquals("", result); + } }