jps -l参看:https://blog.csdn.net/Dongguabai/article/details/82793178
转自:https://blog.csdn.net/derekjiang/article/details/7162415
首先,从JDK1.5之后,Java开始提供包:java.lang.management
java.lang.management 提供了一系列的用来在运行时管理和监督JVM和OS的管理接口。
今天我将用到的就是这个包中的一个类:ManagementFactory。
获取pid的程序代码如下:
// get name representing the running Java virtual machine.
String name = ManagementFactory.getRuntimeMXBean().getName();
System.out.println(name);
// get pid
String pid = name.split("@")[0];
System.out.println("Pid is:" + pid);
输出:
25107@abc.mmm.xxx.yyy.com
Pid is:25107
第一行打印的是代表运行时JVM的一个名字,我们可以看到,这个名字是以进程pid开头,以机器名结尾,中间用“@”连接而成的。
因此我们就可以从这个名字当中,截取出我们所需的pid了。
当然,这只是java.lang.management包中的一个小功能,该包还提供了很多其他的管理接口,参照java doc如下:
Interface Summary | |
---|---|
ClassLoadingMXBean | The management interface for the class loading system of the Java virtual machine. |
CompilationMXBean | The management interface for the compilation system of the Java virtual machine. |
GarbageCollectorMXBean | The management interface for the garbage collection of the Java virtual machine. |
MemoryManagerMXBean | The management interface for a memory manager. |
MemoryMXBean | The management interface for the memory system of the Java virtual machine. |
MemoryPoolMXBean | The management interface for a memory pool. |
OperatingSystemMXBean | The management interface for the operating system on which the Java virtual machine is running. |
RuntimeMXBean | The management interface for the runtime system of the Java virtual machine. |
ThreadMXBean | The management interface for the thread system of the Java virtual machine. |