Mastering Arthas Redefine: Live Java Class Hot‑Update Made Simple

This article explains how Arthas's redefine command enables hot‑updating of Java classes at runtime, outlines scenarios where redefinition fails, offers tips for uploading compiled .class files, and provides a concrete code example demonstrating the process.

FunTester
FunTester
FunTester
Mastering Arthas Redefine: Live Java Class Hot‑Update Made Simple
Arthas is an open‑source Java diagnostic tool.

The redefine command in Arthas loads an external .class file and replaces the already loaded class in the JVM, effectively achieving Java hot‑update without restarting the application.

Redefinition will fail in three situations: (1) when a new field is added, (2) when a new method is added, and (3) when attempting to replace a method that is currently executing; the latter requires the method to finish before the replacement can take effect, similar to an infinite‑loop method that never returns.

A practical tip is to upload the local .class file to the server for hot‑update; instead of manual upload, you can use the mc command on the server to compile the updated code and replace it locally.

Arthas command redefine for Java hot‑update

package com.fun;

import com.fun.frame.SourceCode;
import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.List;

public class Fun extends SourceCode {
    int[] ss = new int[1024];
    public static Logger logger = getLogger(Fun.class);

    public static void main(String[] args) {
        List<Fun> funs = new ArrayList<>();
        while (true) {
            Fun fun = new Fun();
            funs.add(fun);
            sleep(3000);
            test();
            output(funs.size());
        }
    }

    public static void test() {
        logger.info("成功!!!");
    }
}

Calling the test() method prints a log; after redefining the method, any subsequent calls will reflect the updated implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingJavaJVMArthasHot UpdateRedefine
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.