如何使用java 9终止当前进程

时间:2017-03-11 14:07:57

标签: java-9 processhandle

我使用以下代码尝试了destroydestroyForcibly -

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

但在两种情况下都是

  

线程中的异常" main" java.lang.IllegalStateException:destroy of   目前的流程不允许   java.base / java.lang.ProcessHandleImpl.destroyProcess(ProcessHandleImpl.java:308)     在   java.base / java.lang.ProcessHandleImpl.destroyForcibly(ProcessHandleImpl.java:331)     在util.CurrentProcess.main(CurrentProcess.java:18)

1 个答案:

答案 0 :(得分:5)

Javadoc on ProcessHandle::current说:

  

返回当前进程的ProcessHandleProcessHandle不能用于销毁当前流程,请改用System.exit