APM-Plugin: java.lang.ClassNotFoundException: net.bytebuddy.matcher.ElementMatcher

I am trying to create a plugin for the elastic apm-agent-java.

I have closely followed the external plugin example: apm-agent-java/integration-tests/external-plugin-test/external-plugin at master · elastic/apm-agent-java · GitHub

However, I am getting an exception when trying to attach the agent:

2021-10-28 08:54:14,082 [main] INFO  co.elastic.apm.agent.bci.ElasticApmAgent -
Loading plugin apm-test-plugin-1.0.0-SNAPSHOT.jar
[elastic-apm-agent] ERROR Failed to start agent
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at co.elastic.apm.agent.premain.AgentMain.loadAndInitializeAgent(AgentMain.java:149)
        at co.elastic.apm.agent.premain.AgentMain.init(AgentMain.java:94)
        at co.elastic.apm.agent.premain.AgentMain.premain(AgentMain.java:50)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:513)
        at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)
Caused by: java.lang.NoClassDefFoundError: net/bytebuddy/matcher/ElementMatcher
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:398)
        at co.elastic.apm.agent.util.DependencyInjectingServiceLoader.instantiate(DependencyInjectingServiceLoader.java:128)
        at co.elastic.apm.agent.util.DependencyInjectingServiceLoader.instantiate(DependencyInjectingServiceLoader.java:118)
        at co.elastic.apm.agent.util.DependencyInjectingServiceLoader.<init>(DependencyInjectingServiceLoader.java:69)
        at co.elastic.apm.agent.util.DependencyInjectingServiceLoader.load(DependencyInjectingServiceLoader.java:89)
        at co.elastic.apm.agent.bci.ElasticApmAgent.loadInstrumentations(ElasticApmAgent.java:171)
        at co.elastic.apm.agent.bci.ElasticApmAgent.initInstrumentation(ElasticApmAgent.java:163)
        at co.elastic.apm.agent.bci.ElasticApmAgent.initialize(ElasticApmAgent.java:149)
        ... 13 more
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.matcher.ElementMatcher
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 22 more

Any ideas what is going wrong?

Thank you!

I think I have figured it out:

integration-tests/external-plugin-test/external-plugin/pom.xml was recently changed (after the latest release).

Before the change, it contained the following configuration for the maven-shade-plugin:

 <relocations>
    <relocation>
      <pattern>net.bytebuddy</pattern>
      <shadedPattern>co.elastic.apm.agent.shaded.bytebuddy</shadedPattern>
   </relocation>
</relocations>

Adding these lines to my plugin seems to fix the issue.

Yes, this will change in the next version of the agent. The current version requires Byte Buddy to be shaded. The next version requires it not to be shaded.

We're working towards a more stable API so that there won't be breaking changes after the plugin API has been declared stable.

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.