如何使用 java 9 终止当前进程
How to kill the current process with java 9
我已经尝试使用下面的代码 destroy
和 destroyForcibly
-
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("current process id:"+currentProcess.getPid());
currentProcess.destroyForcibly();
但在这两种情况下都出现异常
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)
Javadoc on ProcessHandle::current
说:
Returns a ProcessHandle
for the current process. The ProcessHandle
cannot be used to destroy the current process, use System.exit
instead.
我已经尝试使用下面的代码 destroy
和 destroyForcibly
-
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("current process id:"+currentProcess.getPid());
currentProcess.destroyForcibly();
但在这两种情况下都出现异常
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)
Javadoc on ProcessHandle::current
说:
Returns a
ProcessHandle
for the current process. TheProcessHandle
cannot be used to destroy the current process, useSystem.exit
instead.