java.lang.NoClassDefFoundError: net/bytebuddy/implementation/Implementation$Context$Factory

java - 调用通过 ByteBuddy 检测的 java 代理时出现异常

我正在尝试重新创建 Will 的 blog post 中描述的行为但尝试通过以下方式运行它时出现以下异常:

$ java -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar -jar example/target/securi 
tyfixer-example-1.0-SNAPSHOT.jar                                                                                                                           
Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/implementation/Implementation$Context$Factory                                     
        at java.lang.Class.getDeclaredMethods0(Native Method)                                                                                             
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)                                                                                       
        at java.lang.Class.getDeclaredMethod(Unknown Source)                                                                                               
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)                                                                       
        at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)                                                                     
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.implementation.Implementation$Context$Factory                                                   
        at java.net.URLClassLoader.findClass(Unknown Source)                                                                                               
        at java.lang.ClassLoader.loadClass(Unknown Source)                                                                                                 
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)                                                                                     
        at java.lang.ClassLoader.loadClass(Unknown Source)                                                                                                 
        ... 5 more                                                                                                                                         
FATAL ERROR in native method: processing of -javaagent failed 

structure正如 Will 的博客中所述 - 3 个独立的 jar,一个包含代理,一个包含拦截器,一个包含 Main 类。

我还尝试通过将 mainClass 节添加到 securityfixer-example 的 list 中来将其作为可执行 jar 运行,但这似乎完全绕过了检测:

$ java -jar example/target/securityfixer-example-1.0-SNAPSHOT.jar -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar 
Security manager is set! 
ATTACK SUCCEEDED: Security manager was reset! 

我在这里可能缺少什么?提前致谢。

最佳答案

以下设置似乎有效:

byte-buddy-1.0.0.jar必须在 java-agents-experiments\securityfixer\agent\target 内以及生成的 securityfixer-agent-1.0-SNAPSHOT.jar因为后者取决于前者。这是通过在 securityfixer-agent/pom.xml 中包含以下执行复制的插件来实现的。 :

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

以及<Boot-Class-Path>中的以下引用到上述节生成的工件:

<plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Can-Redefine-Classes>true</Can-Redefine-Classes>
                        <Can-Retransform-Classes>true</Can-Retransform-Classes>
                        <Agent-Class>com.excelsiorsoft.securityfixer.agent.SecurityFixerAgent</Agent-Class>
                        <Premain-Class>com.excelsiorsoft.securityfixer.agent.SecurityFixerAgent</Premain-Class>
                        <Boot-Class-Path>byte-buddy-1.0.0.jar</Boot-Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

这样,不必要的依赖项(例如 securityfixer-bootstrap )就不会被上述插件与 byte-buddy-1.0.0.jar 一起复制。我需要将它们的范围更改为 provided 。 maven-dependency-plugin似乎跳过将该范围的依赖项复制到其目标文件夹。

为了能够将其作为可执行 jar 运行,我们需要添加 <mainClass>节至securityfixer-example/pom.xml :

<plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>securityfixer.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
$ java -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar -jar example/target/securit yfixer-example-1.0-SNAPSHOT.jar


Security manager is set!    
ATTACK FAILED: SecurityManager cannot be reset!

java 

// 如果需要在 ide 中 debug, 需要加上此命令

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 \

// agent包的绝对路径

-javaagent:/dist/agent-core.jar \

-Dserver.port=7001 \

-jar target/spring-boot-demo.jar

参考:使用 byte buddy 的 Advice 时,抛出 java.lang.NoClassDefFoundError 异常答案 - 爱码网

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware是一种Java运行时错误。它表示在动态运行时,JVM无法找到org.springframework.context.ApplicationContextAware类。可能的原因是该类在Javaclasspath路径中不可用。 要解决这个问题,你可以尝试以下几个步骤: 1. 确保org.springframework.context.ApplicationContextAware类的正确版本存在于Javaclasspath路径中。 2. 检查你的程序是否正确地引入了Spring框架,并且版本与org.springframework.context.ApplicationContextAware类对应。 3. 检查你的构建工具(如Maven或Gradle)是否正确地配置了依赖项,并且能够正确地将org.springframework.context.ApplicationContextAware类添加到classpath路径中。 4. 检查你的程序是否正确地配置了Spring上下文,并且ApplicationContextAware接口的实现类正确地注册到了Spring容器中。 如果你已经尝试了上述步骤但问题仍然存在,你还可以考虑以下可能的原因: 1. 你的程序的启动脚本可能覆盖了原来的classpath环境变量,导致org.springframework.context.ApplicationContextAware类不可见。 2. 检查日志文件是否有其他的错误,如java.lang.ExceptionInInitializerError。NoClassDefFoundError有时也可能是由于静态初始化失败导致的。 3. 如果你的程序工作在J2EE的环境中,并且使用了多个不同的类加载器,那么NoClassDefFoundError也可能是由于类加载器的问题导致的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值