bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Java [Java] 取得程式執行時的 PID
查看: 3254|回復: 0
go

[Java] 取得程式執行時的 PID [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2011-9-1 11:30 |只看該作者 |倒序瀏覽 |打印
取得程式執行時的 Process ID

下列程式在 Windows 與 Linux 環境中都實際執行過
  1. import java.lang.management.ManagementFactory;

  2. public class GetPID {

  3.     private static int getPID() {
  4.         int result = -1;
  5.         String name = ManagementFactory.getRuntimeMXBean().getName(); //format pid@hostname
  6.         try {
  7.             result = Integer.parseInt(name.substring(0, name.indexOf('@')));
  8.         } catch (Exception e) {
  9.         }
  10.         return result;
  11.     }

  12.     public static void main(String[] args) {
  13.         System.out.println(getPID());
  14.     }
  15. }
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-4-20 18:35 , Processed in 0.024246 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.