java 进程id_使用Java代码获取Java进程ID的方法

需要jre/lib下的tools.jar包

public class Test {

public static void main(String[] args) throws Exception {

// System.out.println(getPidFromWindows("javaw"));

// 获取监控主机

/*MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");

// 取得所有在活动的虚拟机集合

Set vmlist = new HashSet(local.activeVms());

// 遍历集合,输出PID和进程名

for (Object process : vmlist) {

MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));

// 获取类名

String processname = MonitoredVmUtil.mainClass(vm, true);

System.out.println(process + " ------> " + processname);

}

List list = new ArrayList();

System.out.println(list.size());*/

List list = getPid();

System.out.println(list);

}

public static List getPid(){

List list = new ArrayList();

try {

// 获取监控主机

MonitoredHost local;

local = MonitoredHost.getMonitoredHost("localhost");

// 取得所有在活动的虚拟机集合

Set vmlist = new HashSet(local.activeVms());

// 遍历集合,输出PID和进程名

for (Object process : vmlist) {

MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));

// 获取类名

String processname = MonitoredVmUtil.mainClass(vm, true);

System.out.println(process + " ------> " + processname);

if (processname.endsWith("stable.jar")) {

list.add(((Integer)process).toString());

}

}

} catch (MonitorException e) {

e.printStackTrace();

} catch (URISyntaxException e) {

e.printStackTrace();

}

return list;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值