Skip to content

Commit 9715930

Browse files
committed
Attempted fix for windows killing subprocess
1 parent 2ea806a commit 9715930

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

java/runner/java/com/senzing/runner/SnippetRunner.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ public class SnippetRunner {
3535

3636
private static final int SIGTERM_EXIT_CODE = 143;
3737

38+
private static final boolean WINDOWS;
39+
private static final boolean MACOS;
40+
41+
static {
42+
final String osName = System.getProperty("os.name");
43+
44+
boolean windows = false;
45+
boolean macOS = false;
46+
47+
String lowerOSName = osName.toLowerCase().trim();
48+
if (lowerOSName.startsWith("windows")) {
49+
windows = true;
50+
} else if (lowerOSName.startsWith("mac") || lowerOSName.indexOf("darwin") >= 0) {
51+
macOS = true;
52+
}
53+
54+
WINDOWS = windows;
55+
MACOS = macOS;
56+
}
57+
3858
/**
3959
* Harness for running one or more of the code snippets.
4060
*
@@ -321,7 +341,8 @@ private static void executeSnippet(String snippet,
321341
System.out.println("Runner destroying " + snippet + " process...");
322342
// NOTE: using process.destroy() does not trigger the registered
323343
// shutdown hooks in the snippet sub-process for some reason
324-
Process killer = runtime.exec("kill " + process.pid());
344+
Process killer = runtime.exec(
345+
((WINDOWS) ? "taskkill /F /PID " : "kill ") + process.pid());
325346
Thread killerr = startOutputThread(killer.getErrorStream(), System.err);
326347
Thread killout = startOutputThread(killer.getInputStream(), System.out);
327348
killer.waitFor(); // wait for the kill process to complete
@@ -377,27 +398,15 @@ private static void printUsage(SortedMap<String, SortedSet<String>> snippetMap)
377398

378399
private static String getJarPath() throws RuntimeException {
379400
try {
380-
final String osName = System.getProperty("os.name");
381-
382-
boolean windows = false;
383-
boolean macOS = false;
384-
385-
String lowerOSName = osName.toLowerCase().trim();
386-
if (lowerOSName.startsWith("windows")) {
387-
windows = true;
388-
} else if (lowerOSName.startsWith("mac") || lowerOSName.indexOf("darwin") >= 0) {
389-
macOS = true;
390-
}
391-
392401
String resourceName = SnippetRunner.class.getSimpleName() + ".class";
393402
String url = SnippetRunner.class.getResource(resourceName).toString();
394403
String jarPath = url.replaceAll("jar:file:(.*\\.jar)\\!/.*\\.class", "$1");
395404

396-
if (windows && jarPath.startsWith("/")) {
405+
if (WINDOWS && jarPath.startsWith("/")) {
397406
jarPath = jarPath.replaceAll("[/]+([^/].*)", "$1");
398407
}
399408

400-
if (windows && jarPath.startsWith("/")) {
409+
if (WINDOWS && jarPath.startsWith("/")) {
401410
jarPath = jarPath.substring(1);
402411
}
403412
return jarPath;

0 commit comments

Comments
 (0)