简体   繁体   English

ByteBuddy java 代理导致 noclassdeffound 错误

[英]ByteBuddy java agent results in noclassdeffound error

I get the following error for a simple agent written with Bytebuddy.对于使用 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.我有一个简单的演示应用程序,它基于 spring 引导并尝试使用下面的简单 java 代理代码片段运行它。

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在 Intellij 中时出现以下错误

"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.我假设您创建了一个 jar 文件,其中包含作为代理的类并包含premain方法。 Since you are using Maven, your project dependencies will by default not be contained in this created jar file.由于您使用的是 Maven,因此默认情况下,您的项目依赖项将不包含在此创建的 jar 文件中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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