LoginSignup
22
24

More than 5 years have passed since last update.

[Java] 自分自身のプロセスIDを取得する方法

Last updated at Posted at 2014-06-25

自分自身のプロセスIDを取得する方法

Java ではプロセスIDを簡単に取得する事ができない様ですね。
Windows や Linux 等で動作する方法を、結構探しました。

/**
 * process id (pid).
 */
public class ProcessIdSample {
  /**
   * process id (pid).
   */
  static final String PID =
    java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split("@")[0];

  /**
   * print process id (pid).
   *
   * @param args
   *            String...
   */
  public static void main(String... args) {
    System.out.println("PID: " + PID);
  }
}

プロセスIDは、static final な変数に保持する方が良いですよね。
何度も計算することじゃないし。

もっと、いいプロセスIDの取得方法があれば教えてください。

22
24
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
24