eclipse 如何在调试模式下运行时修改 Java 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12661114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify Java code while running in debug mode?
提问by David Weng
How can I enable this "Debugging in Runtime" Notch is talking about in this videoin Eclipse?
如何在 Eclipse 中启用 Notch 在此视频中谈论的“运行时调试” ?
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.
作为测试,我希望能够编辑以下代码的输出,并在运行时将其更改为“Hello Runtime Debugging”。
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.
编辑:我修改了代码,现在我得到了我想要的结果。Suraj Chandran 下面的回答对此进行了解释。
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);
}
回答by Suraj Chandran
Eclipse supports hot swapping code during debugging , out of the box.
Eclipse 支持在调试期间开箱即用的热交换代码。
While debugging, just change any code and save it, Eclipse will automatically transfer the modified code to the target VM.
调试时,只要修改任何代码并保存,Eclipse 会自动将修改后的代码传输到目标虚拟机。
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.
编辑:请注意,在调试期间更改代码将使该方法从头开始重新执行,重置该方法中的局部变量。
回答by user4049098
You need to make sure that Project > Build Automaticallyis checked. Otherwise it might not work.
您需要确保选中Project > Build Automatically。否则它可能不起作用。
回答by u5156224
After enable Project-> Build Automatically, hot swapping code in debug mode is ok to me
启用 Project-> Build Automatically 后,调试模式下的热插拔代码对我来说是可以的
回答by azendh
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.
我可能误解了这个问题,但是如果您在 Eclipse 中以调试模式(运行/调试)运行程序,您可以在程序运行期间编辑方法的内容(如果 JVM 支持)。通常,您不能更改导入、方法签名、类定义等,只能更改方法的内容。