linux中如何杀死java进程,我如何用SIGKILL杀死java进程中的Linux进程Process.destroy()做SIGTERM...

这篇博客介绍了如何在Java中获取UNIX进程的PID以及使用PID来杀死进程的方法。通过反射访问`UNIXProcess`类的私有字段来获取PID,并使用`Runtime.getRuntime().exec()`执行`kill`命令。
摘要由CSDN通过智能技术生成

斯蒂芬他的回答是正确的。我写的是什么,他说:

public static int getUnixPID(Process process) throws Exception

{

System.out.println(process.getClass().getName());

if (process.getClass().getName().equals("java.lang.UNIXProcess"))

{

Class cl = process.getClass();

Field field = cl.getDeclaredField("pid");

field.setAccessible(true);

Object pidObject = field.get(process);

return (Integer) pidObject;

} else

{

throw new IllegalArgumentException("Needs to be a UNIXProcess");

}

}

public static int killUnixProcess(Process process) throws Exception

{

int pid = getUnixPID(process);

return Runtime.getRuntime().exec("kill " + pid).waitFor();

}

您还可以得到PID这样:

public static int getPID() {

String tmp = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();

tmp = tmp.split("@")[0];

return Integer.valueOf(tmp);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值