使用RuntimeMXBean获取PID_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用RuntimeMXBean获取PID

使用RuntimeMXBean获取PID

 2013/8/18 21:29:24  MouseLearnJava  程序员俱乐部  我要评论(0)
  • 摘要:java.lang.management.RuntimeMXBean是Java虚拟机的运行时系统的管理接口。使用它可以获取正在运行的Java虚拟机等信息,包括获取PID。以下就是一个获取PID的例子:importjava.lang.management.ManagementFactory;importjava.lang.management.RuntimeMXBean;importjava.util.logging.Logger;publicclassRuntimeMXBeanUtil
  • 标签:使用

java.lang.management.RuntimeMXBean是Java 虚拟机的运行时系统的管理接口。使用它可以获取正在运行的 Java 虚拟机等信息,包括获取PID。

以下就是一个获取PID的例子

class="java">import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.logging.Logger;

public class RuntimeMXBeanUtil {
	
	private static final Logger logger = MyLoggerUtil.setLoggerHanlder(Logger.getLogger("RuntimeMXBeanUtil"));
	
	private static final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
	
	public static void main(String[] args) {
		
		logger.info("BootClassPart-->\n"+runtimeMXBean.getBootClassPath());
		//logger.info("ManagerSpecVersion is "+runtimeMXBean.getManagementSpecVersion());
		//logger.info("ClassPath is "+runtimeMXBean.getClassPath());
		//logger.info("Library path is "+runtimeMXBean.getLibraryPath());
		
		/**
		 * Populate the process ID
		 */
		logger.info("The PID is "+populateProcessID());
		
		logger.info("Spec Name is "+runtimeMXBean.getSpecName());
		logger.info("Spec Vendor is "+runtimeMXBean.getSpecVendor());
		//logger.info("Spec Version is "+runtimeMXBean.getSpecVersion());
		//logger.info("Start Time is "+runtimeMXBean.getStartTime());
		//logger.info("Start Time is "+runtimeMXBean.getStartTime());
		//logger.info("Update Time is "+runtimeMXBean.getUptime());
		
		logger.info("VM Name is "+runtimeMXBean.getVmName());
		
		logger.info("VM Vendor is "+runtimeMXBean.getVmVendor());
		logger.info("VM Version is "+runtimeMXBean.getVmVersion());
		
		/**
		 * Get System Properties
		 */
		logger.info("System Properties--> "+runtimeMXBean.getSystemProperties());
		
		//logger.info("Is boot class path supported? "+runtimeMXBean.isBootClassPathSupported());
		//System.getProperties();
		//what is the difference between ManagementFactory.getRuntimeMXBean().getSystemProperties() and System.getProperties() ?
		//The difference is you can use RuntimeMXBean from a remote JVM to obtain its system properties.
		
		//System.out.println(runtimeMXBean.getSystemProperties().size() == System.getProperties().size());
		
		javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,
				"The PID is " +populateProcessID() , null, javax.swing.JOptionPane.DEFAULT_OPTION);
	}
	
	private static String populateProcessID()
	{	
		/*
		 * runtimeMXBean.getName()取得的值包括两个部分:PID和hostname,两者用@连接。
		 */
		RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
		return runtimeMXBean.getName().split("@")[0];
	}
}


控制台输出信息如下:


在任务管理器中查找获取的PID。



  • 大小: 68.2 KB
  • 大小: 68.5 KB
  • 查看图片附件
发表评论
用户名: 匿名