How can a Java program get its own process ID?

后端 未结 22 2451
梦毁少年i
梦毁少年i 2020-11-22 03:47

How do I get the id of my Java process?

I know there are several platform-dependent hacks, but I would prefer a more generic solution.

22条回答
  •  暖寄归人
    2020-11-22 04:30

    This is what I used when I had similar requirement. This determines the PID of the Java process correctly. Let your java code spawn a server on a pre-defined port number and then execute OS commands to find out the PID listening on the port. For Linux

    netstat -tupln | grep portNumber
    

提交回复
热议问题