6

A new feature of Java 9 is that it can not only forcefully kill processes (in the meaning of SIGKILL) it had created but it may also support to send a SIGTERM (in Java called "normal termination").

According to the documentation of Process one can query if the implementation supports this:

public boolean supportsNormalTermination​() Returns true if the implementation of destroy() is to normally terminate the process, Returns false if the implementation of destroy forcibly and immediately terminates the process. Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) return true or false depending on the platform implementation.

I made some tests using Oracle JRE 9.0.1 (Windows 64bit):

Process p = Runtime.getRuntime().exec("javaw -cp target/classes TestClassWait10Minutes");
p.waitFor(5, TimeUnit.SECONDS);
System.out.println(p.supportsNormalTermination());

However it seems like Oracle JRE does not supports "normal termination" as I always get false.

As it depends on the "platform implementation" it seems that Java 9 defines it but does not implement it.

Does anybody know if the feature of "normal termination" is usable at all in any available Java VM?

Naman
  • 27,789
  • 26
  • 218
  • 353
JMax
  • 1,134
  • 1
  • 11
  • 20
  • 1
    I don't terminate the process, I just wit for 5 seconds. The java program "TestClass" runs 10 minutes. – JMax Nov 13 '17 at 14:46

1 Answers1

5

On Linux implementation, it seems to support normal termination. From the source of ProcessImpl:

// Linux platforms support a normal (non-forcible) kill signal.
static final boolean SUPPORTS_NORMAL_TERMINATION = true;

...

@Override

public boolean supportsNormalTermination() {
    return ProcessImpl.SUPPORTS_NORMAL_TERMINATION;
}

Not the case on Windows.

M A
  • 71,713
  • 13
  • 134
  • 174
  • 1
    Looks like Oracle did not want to provide a Windows implementation. AFAIK using `GenerateConsoleCtrlEvent` it is possible to send a Ctrl+C/SIGTERM to a Windows process via it's console. – JMax Nov 13 '17 at 15:49
  • @JMax or maybe there's an actual valid reason. But yeah, they probably didn't **want** to. – Kayaman Nov 13 '17 at 16:08
  • 2
    https://bugs.openjdk.java.net/browse/JDK-8056139 probably addressed this, but is still open. – M A Nov 13 '17 at 16:22