解决 Agent JAR loaded but agent failed to initialize

前言

在Java应用中,使用Java Agent进行类转换是一种强大的技术,它允许我们在运行时动态地修改类的行为。然而,在使用Agent进行类转换时,我们可能会遇到各种问题。本文将详细探讨一个典型的错误及其解决方案,帮助开发者更有效地使用Java Agent。

错误分析

在使用agent进行动态替换class时遇到错误 ,遇到了以下错误:

com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize
	at sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:121)
	at com.sun.tools.attach.VirtualMachine.loadAgent(VirtualMachine.java:540)
	at com.style.note.base.instrument.Attache.main(Attache.java:18)
Disconnected from the target VM, address: '127.0.0.1:54359', transport: 'socket'```

这个错误通常表明Agent JAR文件已成功加载,但Agent初始化失败。通过进一步分析,我们发现问题出在类名的指定上。

业务代码分析

我们的源码如下:

public class CustomTransformer implements ClassFileTransformer {

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        ClassPool classPool = ClassPool.getDefault();
        try {
            // 错误的包名
            CtClass ctClass = classPool.get("com.style.note.base.Instrument.Base");
            CtMethod method = ctClass.getDeclaredMethod("process");
            method.insertBefore("{System.out.println(\"start\");}");
            method.insertAfter("{System.out.println(\"end \");}");
            return ctClass.toBytecode();
        } catch (NotFoundException | CannotCompileException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

问题定位

我们发现,CtClass ctClass = classPool.get(“com.style.note.base.Instrument.Base”); 这一行中的包名是错误的。正确的包名应该是 com.style.note.base.instrument.Base。
instrument 包名的首字母应该是小写的

解决方案

更新包名:

将错误的包名 com.style.note.base.Instrument.Base 更新为正确的包名 com.style.note.base.instrument.Base。

检查MANIFEST.MF文件:

确保MANIFEST.MF文件中的类名和Agent类名完全匹配。例如:

Premain-Class: com.style.note.base.instrument.Agent

确保agentmain方法签名正确:

agentmain 方法的签名必须严格遵循以下格式:

public static void agentmain(String args, 
					Instrumentation instrumentation){
}

注意参数类型,args 应为 String 而不是 String[]。

其他导致加载失败的原因

  • MANIFEST.MF文件错误:
    确保MANIFEST.MF文件中的类名完全正确,没有拼写错误。
  • Agent类的方法签名:
    确保 agentmain 方法的签名完全正确,特别是参数类型。
    agent类的方法 agentmain 一定要正确
     public class Agent {
    	 /**
         * agentmain
         *
         * @param args  args  注意参数为String 而不是String数组
         * @param instrumentation instrumentation
         */
        public static void agentmain(String args, Instrumentation instrumentation) {
           //....
        }
    }
    

总结

通过仔细检查和修正包名、MANIFEST.MF文件以及方法签名,我们可以有效地解决在使用Java Agent进行类转换时遇到的问题。这些步骤不仅有助于提高代码的稳定性,还能增强我们对Java Agent技术的理解。


good day !!!

VMware_View问题汇总指南 1、启连接服务器提示无法验证服务器 ............................................................................. 3 2、部署linked clone虚拟桌面失败,提示如下错误提示:“View Composer agent initialization state error (18): Failed to join the domain” ................................................ 4 3、PCoIP连接时不能跨两个显示器显示 ........................................................................... 5 4、确认桌面资源池Desktop Pool存在 .............................................................................. 7 5、访问View Security服务器失败:Give final block not properly padded ...................... 8 6、为View Composer创建一个QuickPrep账户的步骤 ................................................... 8 7、登录到虚拟桌面时,提示“Login in as current user”选项失败 .................................... 9 8、View4.5安装View Agent报错:28051 Shared Access错误 ..................................... 10 9、删除VIEW的孤立的persistent diskVMware .............................................................. 11 10、ThinApp打包和运维最佳实践 ................................................................................... 11 11、手动从VMware View Manager删除linked clones或陈旧的virtual desktop ........ 12 12、尝试在VMware View 5.0的Session Manager对话框里尝试删除Session提示:clock skewed .................................................................................................................. 13 13、VMware View 5.0的限制和Maximum说明 ............................................................. 14 14、远程登录桌面后,黑屏后登出的问题 ..................................................................... 14 15、PCoIP连接View 5.0虚拟桌面超时断开导致无法再次登录的问题 ....................... 15 16、关于View 5.x中Adobe Flash Throttling的参数详解 ............................................... 16 17、解决:Unable to retrieve information from Active Directory for domain null .......... 16 18、2008安装View Connection Server失败:There was an error creating a MS Direct17 19、安装View Connection Server replica失败:Error 28018 ......................................... 18 20、激活了自动登陆 View Client 导致虚拟桌面连接失败的解决方案 ....................... 18 21、删除掉 VMware View Composer(Link
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值