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..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 @@ -46,12 +46,17 @@ public final class AppiumServiceBuilder extends DriverService.Builder serverArguments = new HashMap<>(); private File appiumJS; private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS; @@ -79,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 -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 || @@ -97,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 + @@ -128,6 +144,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,21 +161,25 @@ 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); } - 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(); + } } /**