Skip to content

Commit b661581

Browse files
committed
Merge pull request #255 from TikhomirovSergey/#252-fix
#252 fix
2 parents b442b49 + ad2a55e commit b661581

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ public final class AppiumServiceBuilder extends DriverService.Builder<AppiumDriv
4646

4747
private static final int DEFAULT_APPIUM_PORT = 4723;
4848

49-
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM = "npm -g ls --depth=0";
50-
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN = "npm.cmd -g ls --depth=0";
49+
private static final String NODE_COMMAND_PREFIX = defineNodeCommandPrefix();
50+
51+
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM = NODE_COMMAND_PREFIX +
52+
" npm -g ls --depth=0";
53+
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN = NODE_COMMAND_PREFIX +
54+
" npm.cmd -g ls --depth=0";
5155

5256
private static final int REQUIRED_MAJOR_NODE_JS = 0;
5357
private static final int REQUIRED_MINOR_NODE_JS = 0;
5458

59+
5560
final Map<String, String> serverArguments = new HashMap<>();
5661
private File appiumJS;
5762
private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS;
@@ -79,13 +84,19 @@ private static String getProcessOutput(InputStream stream ) throws IOException {
7984
private static void validateNodeJSVersion(){
8085
Runtime rt = Runtime.getRuntime();
8186
String result = null;
87+
Process p = null;
8288
try {
83-
Process p = rt.exec("node -v");
89+
p = rt.exec(NODE_COMMAND_PREFIX + " node -v");
8490
p.waitFor();
8591
result = getProcessOutput(p.getInputStream());
8692
} catch (Exception e) {
8793
throw new InvalidNodeJSInstance("Node.js is not installed", e);
8894
}
95+
finally {
96+
if (p != null)
97+
p.destroy();
98+
}
99+
89100
String versionNum = result.replace("v","");
90101
String[] tokens = versionNum.split("\\.");
91102
if (Integer.parseInt(tokens[0]) < REQUIRED_MAJOR_NODE_JS ||
@@ -97,13 +108,18 @@ private static void validateNodeJSVersion(){
97108
private static File findNodeInCurrentFileSystem(){
98109
Runtime rt = Runtime.getRuntime();
99110
String instancePath;
111+
Process p = null;
100112
try {
101-
Process p = rt.exec(returnCommandThatSearchesForDefaultNode());
113+
p = rt.exec(returnCommandThatSearchesForDefaultNode());
102114
p.waitFor();
103115
instancePath = getProcessOutput(p.getInputStream());
104116
} catch (Exception e) {
105117
throw new RuntimeException(e);
106118
}
119+
finally {
120+
if (p != null)
121+
p.destroy();
122+
}
107123

108124
File result;
109125
if (StringUtils.isBlank(instancePath) || !(result = new File(instancePath + File.separator + NODE_MODULES_FOLDER +
@@ -128,6 +144,13 @@ private static void validateNodeStructure(File node){
128144
absoluteNodePath + "doesn't match " + APPIUM_NODE_MASK);
129145
}
130146

147+
private static String defineNodeCommandPrefix() {
148+
if (Platform.getCurrent().is(Platform.WINDOWS))
149+
return "cmd.exe /C";
150+
else
151+
return "/bin/bash -l -c";
152+
}
153+
131154
public AppiumServiceBuilder() {
132155
usingPort(DEFAULT_APPIUM_PORT);
133156
}
@@ -138,21 +161,25 @@ protected File findDefaultExecutable() {
138161
Runtime rt = Runtime.getRuntime();
139162
Process p;
140163
try {
141-
p = rt.exec("node");
164+
p = rt.exec(NODE_COMMAND_PREFIX + " node");
142165
} catch (IOException e) {
143166
throw new RuntimeException(e);
144167
}
145-
OutputStream outputStream = p.getOutputStream();
146-
PrintStream out = new PrintStream(outputStream) ;
147-
out.println("console.log(process.execPath);") ;
148-
out.close();
149168

150169
try {
170+
OutputStream outputStream = p.getOutputStream();
171+
PrintStream out = new PrintStream(outputStream) ;
172+
out.println("console.log(process.execPath);") ;
173+
out.close();
174+
151175
return new File(getProcessOutput(p.getInputStream()));
152176
}
153177
catch (Throwable t){
154178
throw new RuntimeException(t);
155179
}
180+
finally {
181+
p.destroy();
182+
}
156183
}
157184

158185
/**

0 commit comments

Comments
 (0)