How a Java program get its own process ID

To get Process Id of a java program in java we can use RuntimeMXBean

For Example:

public class TestExample {

public static void main(String[] args) throws InterruptedException {

 System.out.println(ManagementFactory.getRuntimeMXBean().getName());
 //or
 RuntimeMXBean mxbean= ManagementFactory.getRuntimeMXBean();
 System.out.println(mxbean.getName());

  }
}

Output:

5992@XXXX

the first part 5992 represents the process Id.

Advertisements
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s