Master Real-Time Java Debugging with Arthas: From Issue Detection to Hot Swapping
This guide explains how to use Arthas for fast problem location in Java services, demonstrates watch commands, shows one‑click installation, and introduces the ArthasHotSwap plugin for hot code deployment while warning against production misuse.
Preface
When a production issue cannot be reproduced in test or pre‑release environments, adding logs across the whole pipeline is inefficient. A powerful debugging tool like Arthas can provide a "god view" of method inputs and outputs to quickly pinpoint problems.
1. Quick Problem Location
1) Locate the interface method
The following sample code checks a video’s status; it returns success only when both approval and status checks are true.
<code>@Override
public SingleResponse<Boolean> check(Long videoId) {
boolean approvalCheck = this.approvalCheck(videoId);
boolean statusCheck = this.statusCheck(videoId);
return SingleResponse.of(approvalCheck && statusCheck);
}
private Boolean statusCheck(Long videoId) {
// simulate status check
return Boolean.TRUE;
}
private Boolean approvalCheck(Long videoId) {
// simulate approval check
return Boolean.FALSE;
}</code>The symptom is that testers see a true video status but the interface returns false, with no logs to indicate the failing line. Using Arthas’s
watchcommand can observe method parameters and return values.
2) Generate the watch command
Install the "Arthas IDEA" plugin (see image) and right‑click the target method to select watch .
3) Run Arthas
Arthas can be installed on Linux/Unix/Mac with a single command:
<code>curl -L https://arthas.aliyun.com/install.sh | sh</code>The script downloads
as.sh; place it in
$PATHand run
./as.shto enter the interactive console.
4) Execute the watch command
<code>watch com.*.*.app.service.AssetServiceImpl check '{params,returnObj,throwExp}' -n 5 -x 3 'params[0]==123'</code>Explanation:
com.*.*.app.service.AssetServiceImpl : fully qualified class name.
check : method to monitor.
{params,returnObj,throwExp} : observe parameters, return value, and thrown exception.
-n 5 : stop after 5 observations.
-x 3 : limit printed nested structure to 3 levels.
'params[0]==123' : condition filter.
5) Re‑request
With
params[0]=123, the method returns
data=false. The watch output reveals that
approvalCheckreturns false, which is the root cause.
2. Code Hot Deployment
After identifying a code issue, instead of redeploying after a long wait, use the ArthasHotSwap plugin. Install it from the IDEA plugin marketplace.
Workflow: modify code → local compile → select Swap this class → log into remote server → paste generated command → hot‑swap succeeds.
Warning: Never perform hot updates in production. Hot‑swap is a development‑efficiency tool, not a solution for live issues.
The plugin relies on Arthas’s
redefinecommand, which uses the JDK Instrumentation API. Limitations include inability to rename classes or methods, add new fields, add non‑static methods, or modify inner‑class bytecode.
3. Summary
The examples above cover only a fraction of Arthas’s capabilities. For deeper learning, consult the official documentation and GitHub repositories.
Reference Links
https://arthas.gitee.io https://www.yuque.com/arthas-idea-plugin/help/pe6i45 https://github.com/alibaba/arthas/issues?q=label%3Auser-case
Dangbei Technology Team
Dangbei Technology Team public account
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.