bestlong 怕失憶論壇

標題: [Java] 取得程式執行時的 PID [打印本頁]

作者: bestlong    時間: 2011-9-1 11:30     標題: [Java] 取得程式執行時的 PID

取得程式執行時的 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. }
複製代碼





歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/) Powered by Discuz! X1.5