From 4c4474647f1929f856ed11c85fdb0180f5ae402b Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Fri, 9 Oct 2015 00:01:39 +0300 Subject: [PATCH 1/2] #252 fix --- .../service/local/AppiumServiceBuilder.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index 4952cff20..2bc35ec94 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -46,12 +46,17 @@ public final class AppiumServiceBuilder extends DriverService.Builder serverArguments = new HashMap<>(); private File appiumJS; private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS; @@ -80,7 +85,7 @@ private static void validateNodeJSVersion(){ Runtime rt = Runtime.getRuntime(); String result = null; try { - Process p = rt.exec("node -v"); + Process p = rt.exec(NODE_COMMAND_PREFIX + " node -v"); p.waitFor(); result = getProcessOutput(p.getInputStream()); } catch (Exception e) { @@ -128,6 +133,13 @@ private static void validateNodeStructure(File node){ absoluteNodePath + "doesn't match " + APPIUM_NODE_MASK); } + private static String defineNodeCommandPrefix() { + if (Platform.getCurrent().is(Platform.WINDOWS)) + return "cmd.exe /C"; + else + return "/bin/bash -l -c"; + } + public AppiumServiceBuilder() { usingPort(DEFAULT_APPIUM_PORT); } @@ -138,7 +150,7 @@ protected File findDefaultExecutable() { Runtime rt = Runtime.getRuntime(); Process p; try { - p = rt.exec("node"); + p = rt.exec(NODE_COMMAND_PREFIX + " node"); } catch (IOException e) { throw new RuntimeException(e); } From ad2a55e651c60dfdb3a6166888910503b924a196 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Fri, 9 Oct 2015 00:37:19 +0300 Subject: [PATCH 2/2] The destroying of processes which aren't needed --- .../service/local/AppiumServiceBuilder.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index 2bc35ec94..3a9131d0e 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -84,13 +84,19 @@ private static String getProcessOutput(InputStream stream ) throws IOException { private static void validateNodeJSVersion(){ Runtime rt = Runtime.getRuntime(); String result = null; + Process p = null; try { - Process p = rt.exec(NODE_COMMAND_PREFIX + " node -v"); + p = rt.exec(NODE_COMMAND_PREFIX + " node -v"); p.waitFor(); result = getProcessOutput(p.getInputStream()); } catch (Exception e) { throw new InvalidNodeJSInstance("Node.js is not installed", e); } + finally { + if (p != null) + p.destroy(); + } + String versionNum = result.replace("v",""); String[] tokens = versionNum.split("\\."); if (Integer.parseInt(tokens[0]) < REQUIRED_MAJOR_NODE_JS || @@ -102,13 +108,18 @@ private static void validateNodeJSVersion(){ private static File findNodeInCurrentFileSystem(){ Runtime rt = Runtime.getRuntime(); String instancePath; + Process p = null; try { - Process p = rt.exec(returnCommandThatSearchesForDefaultNode()); + p = rt.exec(returnCommandThatSearchesForDefaultNode()); p.waitFor(); instancePath = getProcessOutput(p.getInputStream()); } catch (Exception e) { throw new RuntimeException(e); } + finally { + if (p != null) + p.destroy(); + } File result; if (StringUtils.isBlank(instancePath) || !(result = new File(instancePath + File.separator + NODE_MODULES_FOLDER + @@ -154,17 +165,21 @@ protected File findDefaultExecutable() { } catch (IOException e) { throw new RuntimeException(e); } - OutputStream outputStream = p.getOutputStream(); - PrintStream out = new PrintStream(outputStream) ; - out.println("console.log(process.execPath);") ; - out.close(); try { + OutputStream outputStream = p.getOutputStream(); + PrintStream out = new PrintStream(outputStream) ; + out.println("console.log(process.execPath);") ; + out.close(); + return new File(getProcessOutput(p.getInputStream())); } catch (Throwable t){ throw new RuntimeException(t); } + finally { + p.destroy(); + } } /**