miller
发布于

使用 Arthas 热更新 Java 代码

  • 编译好后,提取class文件,

  • 上传到server

  • java -jar arthas-boot.jar attach连接响应进程号后

  • 🔺 热更 redefine /root/xx.class

  • 🔺🔺🔺 推荐使用 retransform。 https://arthas.aliyun.com/doc/retransform.html

不一定全成功

  • 增加类变量 方法
  • 一直运行中的方法 while()

可以在本地修改代码,编译好后再上传到服务器上。有的服务器不允许直接上传文件,可以使用base64命令来绕过。

在本地先转换.class文件为 base64,再保存为 result.txt

base64 < Test.class > result.txt
到服务器上,新建并编辑result.txt,复制本地的内容,粘贴再保存

把服务器上的 result.txt还原为.class

base64 -d < result.txt > Test.class
用 md5 命令计算哈希值,校验是否一致
echo "4oCcV2VsY29tZSB0byBMaW51eOKAnQo=" | base64 --decode


DIFF

redefine 是进行“替换”
retransform 是进行“修改”

The main difference seems to be that when we redefine a class, we supply a byte[] with the new definition out of the blue, whereas when we retransform, we get a byte[] containing the current definition via the same API, and we return a modified byte[].

Therefore, to redefine, we need to know more about the class. With retransform you can do that more directly: just look at the bytecode given, modify it, and return it.

浏览 (357)
点赞
收藏
评论