How to Hot‑Update Java Code in Production Using Arthas
This tutorial demonstrates how to use Alibaba's open‑source Java diagnostic tool Arthas to decompile, modify, and hot‑replace a running class in a production environment, covering the jad, mc, and redefine commands, practical steps, and common pitfalls.
Introduction
This article is the first in the Arthas series. Online problems are often harder to solve than those in a development environment because remote debugging is usually prohibited, so the author prefers using Arthas to locate production issues.
Arthas is an open‑source Java application diagnostic tool from Alibaba.
Arthas can perform many advanced operations; this article focuses on hot‑updating code in a production environment, which should be used cautiously as it may cause problems.
Black‑screen operations may lead to accidental misuse.
They violate safety‑production norms and lack monitoring, rollback, and downgrade capabilities.
However, in some scenarios—such as issues that cannot be reproduced locally or temporary verification of a fix—hot‑update with Arthas can be considered.
Example
In the arthas-demo example there are two classes: HelloService with a sayHello method that prints “hello world”, and a Main class that repeatedly calls it.
public class HelloService {
public void sayHello() {
System.out.println("hello world");
}
} public class Main {
public static void main(String[] args) throws InterruptedException {
HelloService helloService = new HelloService();
while (true) {
Thread.sleep(1000);
helloService.sayHello();
}
}
}Requirement
Assume the program runs online and we want to change the output from “hello world” to “hello arthas”.
Arthas hot‑update consists of three steps:
Use jad to decompile the class in memory and generate a source file.
Edit the source, then compile a new class with mc.
Reload the new class with redefine.
jad Decompile
After attaching Arthas, run:
$ jad --source-only moe.cnkirito.arthas.demo.HelloService > /tmp/HelloService.javaThe generated HelloService.java contains the original source:
package moe.cnkirito.arthas.demo;
public class HelloService {
public void sayHello() {
System.out.println("hello world");
}
}The --source-only flag omits class‑loader information. Edit the file and replace “hello world” with “hello arthas” to prepare for the next step.
sc Find Class Loader
The mc command needs the class‑loader hash, which can be obtained with:
$ sc -d moe.cnkirito.arthas.demo.HelloServiceLook for the line classLoaderHash 18b4aac2 (example value).
mc Memory Compile
Compile the modified source with the hash:
$ mc -c 18b4aac2 /tmp/HelloService.java -d /tmp
Memory compiler output:
/tmp/moe/cnkirito/arthas/demo/HelloService.classredefine Hot‑Update
Reload the new class:
$ redefine /tmp/moe/cnkirito/arthas/demo/HelloService.classVerification
Running the program now prints “hello arthas” after a few iterations, confirming the hot‑update succeeded.
Common Issues
redefine Limitations
Adding or removing fields/methods is not allowed; an error such as “redefine error! … attempted to change the schema” will appear.
Methods already executing will not be affected until the next invocation.
mc Issues
Compilation may fail if the runtime JDK differs from the compile‑time JDK; in that case compile locally and upload the .class file.
When inner classes exist, mc generates multiple .class files; you can pass all of them to redefine at once.
Reference
https://blog.csdn.net/hengyunabc/article/details/87718469
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
