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(); /** * 缓存目录