Replace java class on the fly via Arthas

Startup

Launch arthas and assign the java progress.

$ curl -O https://arthas.aliyun.com/arthas-boot.jar
$ java -jar arthas-boot.jar

[INFO] arthas-boot version: 3.5.3
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 1 app.jar

Or download bin package from Github for offline.

$ unzip arthas-bin.zip -d arthas
$ java -jar arthas/arthas-boot.jar

Decompile class

Decompile class from jvm using jad.
Or jump to the last step to just replace compiled class.

[arthas@1]$ jad --source--only com.package.xxx > xxx.java

Compile java

After changing source file, compile it in memory.

[arthas@1]$ sc -d *xxx | grep classLoader

 classLoaderHash   254989ff

[arthas@1]$ mc -c 254989ff xxx.java -d .

Memory compiler output:
/current-path/xxx.class
Affect(row-cnt:1) cost in 30779 ms.

Redefine class

Redefine compiled class.

[arthas@1]$ redefine /path/to/xxx.class