package com.test; import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; public class Target { public static void main(String[] args) throws InterruptedException { System.out.println(getProcessID()); while(true) { Thread.sleep(10000); } } public static final int getProcessID() { RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); System.out.println(runtimeMXBean.getName()); return Integer.valueOf(runtimeMXBean.getName().split("@")[0]) .intValue(); } }
ManagementFactory 是一个在运行时管理和监控Java VM的工厂类,它能提供很多管理VM的静态接口,比如 RuntimeMXBean ;
RuntimeMXBean 是Java虚拟机的运行时管理接口.
package com.test; import java.util.HashSet; import java.util.Set; import sun.jvmstat.monitor.MonitoredHost; import sun.jvmstat.monitor.MonitoredVm; import sun.jvmstat.monitor.MonitoredVmUtil; import sun.jvmstat.monitor.VmIdentifier; public class ProcessID { public static void main(String[] args) throws Exception { // 获取监控主机 MonitoredHost local = MonitoredHost.getMonitoredHost("localhost"); // 取得所有在活动的虚拟机集合 Set<?> vmlist = new HashSet<Object>(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); } } }
MonitoredHost等类位于${JAVA_HOME}/lib/tools.jar
_
运行结果:
2752 ------> 5172 ------> com.test.Target 5308 ------> com.test.ProcessID
package com.test; import java.net.URISyntaxException; import java.util.HashSet; import java.util.Set; import sun.jvmstat.monitor.MonitorException; import sun.jvmstat.monitor.MonitoredHost; import sun.jvmstat.monitor.MonitoredVm; import sun.jvmstat.monitor.MonitoredVmUtil; import sun.jvmstat.monitor.VmIdentifier; public class ProcessID { public static void main(String[] args) throws Exception { int pid = getProcess(Target.class); System.out.println("PID: "+pid); } public static int getProcess(Class<?> cls) throws MonitorException, URISyntaxException { if(cls == null) { return -1; } // 获取监控主机 MonitoredHost local = MonitoredHost.getMonitoredHost("localhost"); // 取得所有在活动的虚拟机集合 Set<?> vmlist = new HashSet<Object>(local.activeVms()); // 遍历集合,输出PID和进程名 for(Object process : vmlist) { MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process)); // 获取类名 String processname = MonitoredVmUtil.mainClass(vm, true); if(cls.getName().equals(processname)) { return ((Integer)process).intValue(); } } return -1; } }
package com.example.demo; import sun.jvmstat.monitor.*; import java.net.URISyntaxException; import java.util.HashSet; import java.util.Set; public class ProcessID2 { public static void main(String[] args) throws Exception { int pid = getProcess("cn.usr.demo.DemoApplication"); System.out.println("PID: " + pid); } public static int getProcess(String cls) throws MonitorException, URISyntaxException { if (cls == null) { return -1; } // 获取监控主机 MonitoredHost local = MonitoredHost.getMonitoredHost("localhost"); // 取得所有在活动的虚拟机集合 Set<?> vmlist = new HashSet<Object>(local.activeVms()); // 遍历集合,输出PID和进程名 for (Object process : vmlist) { MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process)); // 获取类名 String processname = MonitoredVmUtil.mainClass(vm, true); if (cls.equals(processname)) { return ((Integer) process).intValue(); } } return -1; } }