From 4cf5d3b3a48dbdcbc186e57ad323e2e7593b917a Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Tue, 31 Dec 2019 15:43:13 +0800 Subject: [PATCH] Avoid append spy jar to BootstrapClassLoader multiple times. fix #996 --- .../com/taobao/arthas/agent/AgentBootstrap.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/agent/src/main/java/com/taobao/arthas/agent/AgentBootstrap.java b/agent/src/main/java/com/taobao/arthas/agent/AgentBootstrap.java index ea0b22e7..132b607d 100644 --- a/agent/src/main/java/com/taobao/arthas/agent/AgentBootstrap.java +++ b/agent/src/main/java/com/taobao/arthas/agent/AgentBootstrap.java @@ -71,7 +71,18 @@ public class AgentBootstrap { private static ClassLoader getClassLoader(Instrumentation inst, File spyJarFile, File agentJarFile) throws Throwable { // 将Spy添加到BootstrapClassLoader - inst.appendToBootstrapClassLoaderSearch(new JarFile(spyJarFile)); + ClassLoader parent = ClassLoader.getSystemClassLoader().getParent(); + Class spyClass = null; + if (parent != null) { + try { + parent.loadClass("java.arthas.Spy"); + } catch (Throwable e) { + // ignore + } + } + if (spyClass == null) { + inst.appendToBootstrapClassLoaderSearch(new JarFile(spyJarFile)); + } // 构造自定义的类加载器,尽量减少Arthas对现有工程的侵蚀 return loadOrDefineClassLoader(agentJarFile);