0
votes

I am trying to instrument Java ThreadPoolExecutor class using byte buddy. I am using my own logger to get logs from agent. But when i try to use this logger with Advice its gives following error.

Exception in thread "main" java.lang.NoClassDefFoundError: com/github/shehanperera/threadagent/GetLoggers
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.<clinit>(ThreadPoolExecutor.java)
        at java.util.concurrent.ThreadPoolExecutor.<clinit>(ThreadPoolExecutor.java:550)
        at java.util.concurrent.Executors.newFixedThreadPool(Executors.java:89)
        at com.github.shehanperera.threadpool.RunThreads.main(RunThreads.java:23)

using answer from Rafael Winterhalter for this Slf4j loggers with Byte Buddy , I used following code in agent to load my agent jar to the boot path.

JarFile jarFile = null;
        try {
            jarFile = new JarFile(new File("threadpool-agent-1.0-SNAPSHOT.jar"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        instrumentation.appendToBootstrapClassLoaderSearch(jarFile); 

But now i am getting following error

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
        at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "net.bytebuddy.agent.builder.AgentBuilder$Default.ignore(Lnet/bytebuddy/matcher/ElementMatcher;)Lnet/bytebuddy/agent/builder/AgentBuilder$Ignored;" the class loader (instance of sun/misc/Launcher$AppClassLoader) of the current class, com/github/shehanperera/threadagent/Agent, and the class loader (instance of <bootloader>) for the method's defining class, net/bytebuddy/agent/builder/AgentBuilder$Default, have different Class objects for the type net/bytebuddy/matcher/ElementMatcher used in the signature
        at com.github.shehanperera.threadagent.Agent.premain(Agent.java:33)
        ... 6 more

Any suggestion to solve this problem ?

1

1 Answers

2
votes

It seems as if you are adding the same class to multiple class loaders what can cause problems, especially if you add those classes lazily. Idealy, you use a build tool like the Shade plugin for Maven or the Shadow plugin for Gradle to bundle all of your classes into the jar file that represents the Java agent.