19

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);
}
Lii
  • 11,553
  • 8
  • 64
  • 88
David Weng
  • 4,165
  • 12
  • 42
  • 51

4 Answers4

30

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • How do you do it? I put a break point, let's say when the loop count is at 23, it's going "Hello World 22" "Hello World 23" I change the text to something else, I press resume, now I get "Hello Something else 0" it restarts the program? The count is at 0 all of a sudden. – David Weng Sep 30 '12 at 12:16
  • It doesent restart the program, it will re-execute the current method from begining. So it will reset all the variables again – Suraj Chandran Sep 30 '12 at 12:18
  • Ah that explains it. Would you mind putting it in the answer? – David Weng Sep 30 '12 at 12:23
11

You need to make sure that Project > Build Automatically is checked. Otherwise it might not work.

ekostadinov
  • 6,880
  • 3
  • 29
  • 47
user4049098
  • 111
  • 1
  • 2
2

After enable Project-> Build Automatically, hot swapping code in debug mode is ok to me

张宇鹏
  • 51
  • 2
1

I may misunderstand the question, but if you run a program in Eclipse in debug mode (Run/Debug), you can edit the content of methods during the program runs (if JVM supports it). Regularly you can not change the imports, method signatures, class definitons, etc, just the content of the methods.

azendh
  • 921
  • 8
  • 23