Skip to content

Commit 77e0945

Browse files
committed
Use JVM 9+ process self-PID lookup when necessary & available
1 parent 7475f6f commit 77e0945

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/main/kotlin/tech/httptoolkit/javaagent/AttachMain.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ fun main(args: Array<String>) {
5959

6060
fun getOwnPid(): String {
6161
// This should work in general, but it's implementation dependent:
62-
return ManagementFactory.getRuntimeMXBean().name.split("@")[0]
62+
val pid = ManagementFactory.getRuntimeMXBean().name.split("@")[0]
63+
64+
return if (pid.toLongOrNull() != null) {
65+
pid
66+
} else {
67+
ProcessHandle.current().pid().toString()
68+
}
6369
}
6470

6571
fun getTargets(): List<VirtualMachineDescriptor> {

0 commit comments

Comments
 (0)