获取Pid

版权声明:本文为博主原创文章,未经博主允许不得转载。
之前并不知道Java中如何能够获取当前进程(也就是包含当前Java程序的JVM所在进程)的进程ID,还以为要通过JNI或者通过Runtime.exec执行shell命令等方式才能获取到当前进程的进程ID,今天在偶然中看到一种在Java程序里,获取当前进程ID的方法,记录下来,以后应该会用到:)
首先,从JDK1.5之后,Java开始提供包:java.lang.management
java.lang.management 提供了一系列的用来在运行时管理和监督JVM和OS的管理接口。
今天我将用到的就是这个包中的一个类:ManagementFactory。
获取pid的程序代码如下:
- import sun.management.ManagementFactory;
- // 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. |
获取Pid的更多相关文章
- 根据PID获取进程名&根据进程名获取PID
Liunx中 通过进程名查找进程PID可以通过 pidof [进程名] 来查找.反过来 ,相同通过PID查找进程名则没有相关命令.在linux根目录中,有一个/proc的VFS(虚拟文件系统),系统当 ...
- python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过autopy实现右键菜单和另存为操作
1.参考 利用 Python + Selenium 自动化快速截图 利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素) 使用python获取系统所有进程PID以及进程名 ...
- linux下根据进程名字获取PID,类似pidof(转)
linux有一个命令行工具叫做pidof,可以根据用户输入的进程名字查找到进程号,但有时候我们需要在程序里实现,不想调用system,在查阅了很多版本的pidof源代码后,没有发现一个自己感觉比较好的 ...
- linux根据进程名获取PID
经常需要Kill多个进程,这些进程包含共同的关键字,可以用一条命令Kill掉它们. ps aux | grep "common" |grep -v grep| cut -c 9-1 ...
- Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...
- linux shell 获取进程pid
1.通过可执行程序的程序名称 a.运行程序 b.获取进程id号 c.pidof相关知识:http://www.cnblogs.com/yunsicai/p/3675938.html 2.有些程序需要在 ...
- CMD魔法堂:获取进程路径和PID值的方法集
一.前言 在开发发布更更新工具——更新Weblogic应用模块时,了解到更新Weblogic应用需要先关闭Weblogic应用窗口然后是清缓存.更新应用文件,最后再重启Weblogic应用窗口. ...
- linux命令(26):Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...
- 014-交互式Shell和shell脚本获取进程 pid
Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...
随机推荐
- HDU 5611 Baby Ming and phone number
#include<cstdio> #include<cstring> #include<vector> #include<cmath> #include ...
- jquery中:input和input的区别分析
:input表示选择表单中的input,select,textarea,button元素,input仅仅选择input元素. <html> <head> <style&g ...
- 基于FPGA的OLED真彩色动态图像显示的实现
源:基于FPGA的OLED真彩色动态图像显示的实现 作为第3代显示器,有机电致发光器件(Organic Light Emitting Diode,OLED)由于其主动发光.响应快.高亮度.全视角.直流 ...
- Lua C Api lua_gettable 、lua_settable 、lua_next 使用详解
之前一直没理清lua_gettable和lua_settable的使用,今天理清了,顺便就做下笔记了.1.lua_gettable void lua_gettable (lua_State *L, i ...
- mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: YES)'
就当作自己忘记Mysql密码把,忘记密码的解决方法 一.mysql登录错误mysqladmin: connect to server at 'localhost' failederror: 'Acce ...
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...
- Java序列化与反序列化,文件操作
参考两篇博客: http://blog.csdn.net/moreevan/article/details/6697777 http://blog.csdn.net/moreevan/article/ ...
- html5 安卓拨打电话 发短信
方法一: <input name=”phone_no” format=”*m” value=”13″/> <do type=”option” label=”呼出号”> < ...
- handler的使用
2014-04-15 10:45:06 简单学习了handler的使用. 昨天下载的问题,在手机上正常,在平板上不正常. 怀疑是网络的问题. 一直获得的流为空 2014-04-15 18:10:59 ...
- 7.广播和多播,IGMP协议
1.单播,多播,广播的介绍 1.1.单播(unicast) 单播是说,对特定的主机进行数据传送.例如给某一个主机发送IP数据包.这时候,数据链路层给出的数据头里面是非常具体的目的地址,对于以太网来 说 ...