From 86d1d4bfb7bb88266ea9ed13733071a6f69f8099 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Sun, 30 Sep 2018 15:20:41 +0800 Subject: [PATCH] improve get pid logic --- .../arthas/core/util/ApplicationUtils.java | 32 +++++++++++++++++++ .../taobao/arthas/core/util/Constants.java | 3 +- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/com/taobao/arthas/core/util/ApplicationUtils.java diff --git a/core/src/main/java/com/taobao/arthas/core/util/ApplicationUtils.java b/core/src/main/java/com/taobao/arthas/core/util/ApplicationUtils.java new file mode 100644 index 00000000..8f17aba4 --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/util/ApplicationUtils.java @@ -0,0 +1,32 @@ +package com.taobao.arthas.core.util; + +import java.lang.management.ManagementFactory; + +/** + * + * @author hengyunabc 2018-09-30 + * + */ +public class ApplicationUtils { + + private static String PID = "-1"; + + static { + // https://stackoverflow.com/a/7690178 + String jvmName = ManagementFactory.getRuntimeMXBean().getName(); + int index = jvmName.indexOf('@'); + + if (index > 0) { + try { + PID = Long.toString(Long.parseLong(jvmName.substring(0, index))); + } catch (Throwable e) { + // ignore + } + } + } + + public static String getPid() { + return PID; + } + +} diff --git a/core/src/main/java/com/taobao/arthas/core/util/Constants.java b/core/src/main/java/com/taobao/arthas/core/util/Constants.java index ad44cd31..7a451718 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/Constants.java +++ b/core/src/main/java/com/taobao/arthas/core/util/Constants.java @@ -1,7 +1,6 @@ package com.taobao.arthas.core.util; import java.io.File; -import java.lang.management.ManagementFactory; import com.taobao.arthas.core.view.Ansi; @@ -46,7 +45,7 @@ public interface Constants { /** * 当前进程PID */ - String PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; + String PID = ApplicationUtils.getPid(); /** * 缓存目录