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

**URL:** <https://discuss.elastic.co/t/apm-plugin-java-lang-classnotfoundexception-net-bytebuddy-matcher-elementmatcher/287889>\
**Category:** APM\
**Tags:** java\
**Created:** [October 28, 2021, 7:24am UTC](https://discuss.elastic.co/t/apm-plugin-java-lang-classnotfoundexception-net-bytebuddy-matcher-elementmatcher/287889 "2021-10-28T07:24:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_W](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_w/32/96451_2.png) [@Matthias\_W](https://discuss.elastic.co/u/Matthias_W)\
**Post date:** [October 28, 2021, 7:24am UTC](https://discuss.elastic.co/t/apm-plugin-java-lang-classnotfoundexception-net-bytebuddy-matcher-elementmatcher/287889/1 "2021-10-28T07:24:27Z")

</div>

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](https://github.com/elastic/apm-agent-java/tree/master/integration-tests/external-plugin-test/external-plugin)

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![Matthias\_W](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_w/32/96451_2.png) [@Matthias\_W](https://discuss.elastic.co/u/Matthias_W)\
**Post date:** [October 28, 2021, 8:21am UTC](https://discuss.elastic.co/t/apm-plugin-java-lang-classnotfoundexception-net-bytebuddy-matcher-elementmatcher/287889/2 "2021-10-28T08:21:54Z")

</div>

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:

```auto
 <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.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [October 28, 2021, 10:41am UTC](https://discuss.elastic.co/t/apm-plugin-java-lang-classnotfoundexception-net-bytebuddy-matcher-elementmatcher/287889/3 "2021-10-28T10:41:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 18, 2021, 6:42am UTC](https://discuss.elastic.co/t/apm-plugin-java-lang-classnotfoundexception-net-bytebuddy-matcher-elementmatcher/287889/4 "2021-11-18T06:42:55Z")

</div>

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