diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index 2866b63b8fb716822d765cf69ef0ccb2314e65af..94e034e51a07cbc9c9ec65a03945a064c38d69ce 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -429,15 +429,15 @@ public abstract class ProcessTree implements Iterable, IProcessTree, public synchronized EnvVars getEnvironmentVariables() { if(env !=null) return env; - env = new EnvVars(); - - try + env = new EnvVars(); + + try { env.putAll(p.getEnvironmentVariables()); } catch (WinpException e) { LOGGER.log(FINE, "Failed to get environment variable ", e); - } + } return env; } }); @@ -550,7 +550,7 @@ public abstract class ProcessTree implements Iterable, IProcessTree, try { int pid = getPid(); LOGGER.fine("Killing pid="+pid); - UnixReflection.DESTROY_PROCESS.invoke(null, pid); + UnixReflection.destroy(pid); } catch (IllegalAccessException e) { // this is impossible IllegalAccessError x = new IllegalAccessError(); @@ -605,7 +605,11 @@ public abstract class ProcessTree implements Iterable, IProcessTree, PID_FIELD = clazz.getDeclaredField("pid"); PID_FIELD.setAccessible(true); - DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class); + if (isPreJava8()) { + DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class); + } else { + DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class, boolean.class); + } DESTROY_PROCESS.setAccessible(true); } catch (ClassNotFoundException e) { LinkageError x = new LinkageError(); @@ -621,6 +625,19 @@ public abstract class ProcessTree implements Iterable, IProcessTree, throw x; } } + + public static void destroy(int pid) throws IllegalAccessException, InvocationTargetException { + if (isPreJava8()) { + DESTROY_PROCESS.invoke(null, pid); + } else { + DESTROY_PROCESS.invoke(null, pid, false); + } + } + + private static boolean isPreJava8() { + int javaVersionAsAnInteger = Integer.parseInt(System.getProperty("java.version").replaceAll("\\.", "").replaceAll("_", "").substring(0, 2)); + return javaVersionAsAnInteger < 18; + } }