简体   繁体   中英

ByteBuddy java agent results in noclassdeffound error

I get the following error for a simple agent written with Bytebuddy. I have a simple demo application which is spring boot based and trying to run it with a simple java agent code snippet below.

public static void premain(String agentArgs, Instrumentation inst) {
    System.out.println("Agent Loaded");
    new AgentBuilder.Default()
        .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
        .type(hasSuperType(named("javax.sql.DataSource")))
        .transform((builder, type, classLoader, module) ->
            builder.visit(
                Advice.to(VariableAdvice.class).on(isMethod())
            )
        )
        .installOn(inst);
}

Advice code

import net.bytebuddy.asm.Advice;

public class VariableAdvice {
    @Advice.OnMethodEnter
    static void OnEnter() {
        System.out.println("Hello ByteBuddy");
    }
}

POM

<dependency>
    <groupId>net.bytebuddy</groupId>
    <artifactId>byte-buddy</artifactId>
    <version>1.10.19</version>
</dependency>

more

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Premain-Class>com.abc.Agent</Premain-Class>
                        <Agent-Class>com.abc.Agent</Agent-Class>
                        <!-- <Main-Class>com.abc.Agent</Main-Class>-->
                        <Can-Redefine-Classes>true</Can-Redefine-Classes>
                        <Can-Retransform-Classes>true</Can-Retransform-Classes>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

When in Intellij I get the following error

"C:\Program Files\Java\jdk1.8.0_261_deleteword\bin\java.exe" -
Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/dynamic/DynamicType$Builder
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethod(Class.java:2128)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:327)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.dynamic.DynamicType$Builder
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 5 more
FATAL ERROR in native method: processing of -javaagent failed

I assume that you create a jar file that contains your classes that is your agent and contains the premain method. Since you are using Maven, your project dependencies will by default not be contained in this created jar file.

You will need to create a fat jar (" uber jar ") that also contains Byte Buddy where Maven offers the Maven Shade plugin to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM