免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6185 | 回复: 2
打印 上一主题 下一主题

跨平台获取java进程id(Process ID in Java) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-19 23:30 |只看该作者 |倒序浏览
跨平台获取java进程id(Process ID in Java)







对于不同平台,获取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.     }   
  117. }  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-19 23:31 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-01-23 21:11 |只看该作者
学习了。啊些。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP