Java的jps工具在内部使用哪个API? [英] Which API does Java's jps tool use internally?

查看:482
本文介绍了Java的jps工具在内部使用哪个API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式重新创建jps工具的功能。我需要找出所有Java运行进程及其id,以便我可以附加到该进程(类似于JConsole所做的)。

I need to recreate the functionalities of the jps tool programmatically. I need to find out all Java running processes along with their id so I can attach to that process (similar to what JConsole does).

我认为VirtualMachine API会有所帮助,但是当我运行以下内容时没有得到预期的结果

I thought the VirtualMachine API would help, but did not get expected result when I run the following

public class ProcessList {
    public static void main(String[] args){
        List<VirtualMachineDescriptor> vms = VirtualMachine.list();
        for(VirtualMachineDescriptor vm : vms){
            System.out.println (vm.id());
        }
    }
}

当我运行上面的代码时,它只返回一个ID,但是当我在同一台机器上运行jps时,我会看到其他几个进程。

When I run the code above, it returns just one ID, but when I run jps on the same machine I see several other processes.

推荐答案

jps 使用内部类 - MonitoredHost activeVMs()方法用于获取主机上所有活动VM的列表。你可以参考OpenJDK的 sun.tools.jps.Jps 类的来源,找出 jps 工具在引擎盖下工作。

jps uses an internal class - MonitoredHost of the Oracle/Sun JRE. The activeVMs() method is used to obtain the list of all active VMs on a host. You can refer to the source of the sun.tools.jps.Jps class of OpenJDK, to find out how the jps tool works under the hood.

这篇关于Java的jps工具在内部使用哪个API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆