Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

eclipse - How to modify Java code while running in debug mode?

How can I enable this "Debugging in Runtime" Notch is talking about in this video in Eclipse?

As a test, I'd like to be able to edit the output of the following code and change it to "Hello Runtime Debugging" while it's running.

public class HelloWorld {
    public static void main(String[] args) throws InterruptedException {
        doIt();     
    }

    private static void doIt() throws InterruptedException {
        for (int i = 0; i < 1000; ++i) {
            System.out.println("Hello World " + i);
            Thread.currentThread().sleep(100);
        }
    }
}

EDIT: I modified the code, now I get the results I was looking for. Suraj Chandran's answer below explains it.

private static void doIt() throws InterruptedException {
    for (int i = 0; i < 1000; ++i) {
        print(i);
        Thread.currentThread().sleep(100);
    }
}

private static void print(int i) {
    System.out.println("Hello Sir " + i);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Eclipse supports hot swapping code during debugging , out of the box.

While debugging, just change any code and save it, Eclipse will automatically transfer the modified code to the target VM.

Note that you can't make structural changes to the code, like adding new methods, changing method signature or adding new fields. But you can change the code within a method.

EDIT: Note that changing the code during deubgging will make that method re-execute form the beginning, resetting the local variables in that method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...