3

I have tried with both destroy and destroyForcibly using the below code -

ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("current process id:"+currentProcess.getPid());
currentProcess.destroyForcibly();

but getting an exception in both cases as

Exception in thread "main" java.lang.IllegalStateException: destroy of current process not allowed at java.base/java.lang.ProcessHandleImpl.destroyProcess(ProcessHandleImpl.java:308) at java.base/java.lang.ProcessHandleImpl.destroyForcibly(ProcessHandleImpl.java:331) at util.CurrentProcess.main(CurrentProcess.java:18)

Naman
  • 27,789
  • 26
  • 218
  • 353
Sudarsana Kasireddy
  • 922
  • 1
  • 8
  • 24
  • 2
    Why don't you just use `System.exit`? – Holger Just Mar 11 '17 at 14:15
  • Did your process even start to kill? :/ – minigeek Mar 11 '17 at 14:18
  • Holger, I just want to see how I can kill process with java 9 using process library. Minigeek, Process started and it's up – Sudarsana Kasireddy Mar 11 '17 at 14:28
  • @SudarsanaKasireddy process is supposed to be killed by destroy method..this will work for processbuilder for sure... I don't know abt processhandle :/ and Btw be aware that if the process that you invoke creates new sub-processes, those may not be terminated (seehttp://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4770092). – minigeek Mar 11 '17 at 14:59

1 Answers1

5

Javadoc on ProcessHandle::current says:

Returns a ProcessHandle for the current process. The ProcessHandle cannot be used to destroy the current process, use System.exit instead.

Mirza
  • 53
  • 7
Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255