1616
1717package org .testingisdocumenting .webtau .cli ;
1818
19+ import java .io .IOException ;
20+ import java .lang .reflect .Field ;
21+
1922public class ProcessBackgroundRunResult {
2023 private final Process process ;
2124 private final StreamGobbler outputGobbler ;
2225 private final StreamGobbler errorGobbler ;
2326
27+ private final int pid ;
28+
2429 private final Thread consumeErrorThread ;
2530 private final Thread consumeOutThread ;
2631
@@ -30,6 +35,7 @@ public ProcessBackgroundRunResult(Process process,
3035 Thread consumeErrorThread ,
3136 Thread consumeOutThread ) {
3237 this .process = process ;
38+ this .pid = extractPid (process );
3339 this .outputGobbler = outputGobbler ;
3440 this .errorGobbler = errorGobbler ;
3541 this .consumeErrorThread = consumeErrorThread ;
@@ -43,7 +49,13 @@ public Process getProcess() {
4349 public void destroy () {
4450 outputGobbler .close ();
4551 errorGobbler .close ();
46- process .destroy ();
52+
53+ try {
54+ ProcessUtils .kill (pid );
55+ process .waitFor ();
56+ } catch (InterruptedException | IOException e ) {
57+ throw new RuntimeException (e );
58+ }
4759 }
4860
4961 public StreamGobbler getOutputGobbler () {
@@ -69,4 +81,20 @@ public ProcessRunResult createRunResult() {
6981 outputGobbler .getException (),
7082 errorGobbler .getException ());
7183 }
84+
85+ /**
86+ * we need to support java 8, pid() is added in java 9
87+ * so using hacks to get to pid value
88+ * @param process process
89+ * @return pid
90+ */
91+ private static int extractPid (Process process ) {
92+ try {
93+ Field pidField = process .getClass ().getDeclaredField ("pid" );
94+ pidField .setAccessible (true );
95+ return (int ) pidField .get (process );
96+ } catch (NoSuchFieldException | IllegalAccessException e ) {
97+ throw new RuntimeException (e );
98+ }
99+ }
72100}
0 commit comments