跨平台获取java进程id(Process ID in Java)

本文介绍了一种跨平台获取Java进程ID的方法,并提供了一个实用的工具类实现。该工具类支持Windows和非Windows系统,通过Java管理扩展和操作系统命令来获取进程ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于不同平台,获取java进程id有不同的方法,这个做一个总结,写一个工具类。

这个工具主要进行两种尝试来获得pid:

  • 从 java.lang.management.RuntimeMXBean获得
  • 从操作系统获得
    • windows系统
    • 非windows系统

工具代码:

Java代码  收藏代码
  1. /**  
  2.  * Process ID in Java  
  3.  *   
  4.  * @author lichengwu  
  5.  * @created 2012-1-18  
  6.  *   
  7.  * @version 1.0  
  8.  */   
  9. public   final   class  PID {  
  10.   
  11.     private   static   final  Log log = LogFactory.getLog(PID. class );  
  12.   
  13.     /**  
  14.      * 私有构造方法  
  15.      */   
  16.     private  PID() {  
  17.         super ();  
  18.     }  
  19.   
  20.     /**  
  21.      * 获得java进程id  
  22.      *   
  23.      * @author lichengwu  
  24.      * @created 2012-1-18  
  25.      *   
  26.      * @return java进程id  
  27.      */   
  28.     public   static   final  String getPID() {  
  29.         String pid = System.getProperty("pid" );  
  30.         if  (pid ==  null ) {  
  31.             RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();  
  32.             String processName = runtimeMXBean.getName();  
  33.             if  (processName.indexOf( '@' ) != - 1 ) {  
  34.                 pid = processName.substring(0 , processName.indexOf( '@' ));  
  35.             } else  {  
  36.                 pid = getPIDFromOS();  
  37.             }  
  38.             System.setProperty("pid" , pid);  
  39.         }  
  40.         return  pid;  
  41.     }  
  42.   
  43.     /**  
  44.      * 从操作系统获得pid  
  45.      * <p>  
  46.      * 对于windows,请参考:http://www.scheibli.com/projects/getpids/index.html  
  47.      *   
  48.      * @author lichengwu  
  49.      * @created 2012-1-18  
  50.      *  
  51.      * @return  
  52.      */   
  53.     private   static  String getPIDFromOS() {  
  54.   
  55.         String pid = null ;  
  56.   
  57.         String[] cmd = null ;  
  58.   
  59.         File tempFile = null ;  
  60.   
  61.         String osName = ParameterUtil.getParameter(Parameter.OS_NAME);  
  62.         // 处理windows   
  63.         if  (osName.toLowerCase().contains( "windows" )) {  
  64.             FileInputStream fis = null ;  
  65.             FileOutputStream fos = null ;  
  66.   
  67.             try  {  
  68.                 // 创建临时getpids.exe文件   
  69.                 tempFile = File.createTempFile("getpids"".exe" );  
  70.                 File getpids = new  File(ParameterUtil.getResourcePath( "getpids.exe" ));  
  71.                 fis = new  FileInputStream(getpids);  
  72.                 fos = new  FileOutputStream(tempFile);  
  73.                 byte [] buf =  new   byte [ 1024 ];  
  74.                 while  (fis.read(buf) != - 1 ) {  
  75.                     fos.write(buf);  
  76.                 }  
  77.                 // 获得临时getpids.exe文件路径作为命令   
  78.                 cmd = new  String[] { tempFile.getAbsolutePath() };  
  79.             } catch  (FileNotFoundException e) {  
  80.                 log.equals(e);  
  81.             } catch  (IOException e) {  
  82.                 log.equals(e);  
  83.             } finally  {  
  84.                 if  (tempFile !=  null ) {  
  85.                     tempFile.deleteOnExit();  
  86.                 }  
  87.                 Closer.close(fis, fos);  
  88.             }  
  89.         }  
  90.         // 处理非windows   
  91.         else  {  
  92.             cmd = new  String[] {  "/bin/sh""-c""echo $$ $PPID"  };  
  93.         }  
  94.         InputStream is = null ;  
  95.         ByteArrayOutputStream baos = null ;  
  96.         try  {  
  97.             byte [] buf =  new   byte [ 1024 ];  
  98.             Process exec = Runtime.getRuntime().exec(cmd);  
  99.             is = exec.getInputStream();  
  100.             baos = new  ByteArrayOutputStream();  
  101.             while  (is.read(buf) != - 1 ) {  
  102.                 baos.write(buf);  
  103.             }  
  104.             String ppids = baos.toString();  
  105.             // 对于windows参考:http://www.scheibli.com/projects/getpids/index.html   
  106.             pid = ppids.split(" " )[ 1 ];  
  107.         } catch  (Exception e) {  
  108.             log.error(e);  
  109.         } finally  {  
  110.             if  (tempFile !=  null ) {  
  111.                 tempFile.deleteOnExit();  
  112.             }  
  113.             Closer.close(is, baos);  
  114.         }  
  115.         return  pid;  
  116.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值