How can a Java program get its own process ID?

后端 未结 22 2297
梦毁少年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:29

    In Scala:

    import sys.process._
    val pid: Long = Seq("sh", "-c", "echo $PPID").!!.trim.toLong
    

    This should give you a workaround on Unix systems until Java 9 will be released. (I know, the question was about Java, but since there is no equivalent question for Scala, I wanted to leave this for Scala users who might stumble into the same question.)

提交回复
热议问题