使用RuntimeMXBean获取PID
程序员文章站
2022-05-26 10:25:37
...
java.lang.management.RuntimeMXBean是Java 虚拟机的运行时系统的管理接口。使用它可以获取正在运行的 Java 虚拟机等信息,包括获取PID。
以下就是一个获取PID的例子:
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。
上一篇: ubuntu 卸载/安装 redis
下一篇: Mongodb使用save更新
推荐阅读
-
Android studio怎么使用git获取最新内容然后合并?
-
使用SQL Server 获取插入记录后的ID(自动编号)
-
Android 使用 DowanloadManager 实现下载并获取下载进度实例代码
-
Android开发中使用外部应用获取SD卡状态的方法
-
Android编程使用光线传感器获取光线强弱的方法【LightSensorManager封装类】
-
如何获取百度阅读下载券?百度阅读下载券获得及使用图文教程
-
python使用wmi模块获取windows下的系统信息 监控系统
-
Net微信网页开发 使用微信JS-SDK获取当前地理位置过程详解
-
使用getid3获取音频文件信息
-
python中使用urllib2获取http请求状态码的代码例子