如何在调试模式下运行时修改java代码?

我如何启用这个“在运行时调试”Notch在Eclipse中的这个video中谈到了什么?

作为测试,我希望能够编辑以下代码的输出,并在运行时将其更改为“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); } } } 

编辑:我修改了代码,现在我得到了我正在寻找的结果。 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); } 

Eclipse在调试期间支持热交换代码,开箱即用。

在调试时,只需更改任何代码并保存,eclipse会自动将修改后的代码传输到目标VM。

请注意,您无法对代码进行结构更改,例如添加新方法,更改方法签名或添加新字段。 但是您可以在方法中更改代码。

编辑:请注意,在deubgging期间更改代码将使该方法从头开始重新执行,重置该方法中的局部变量。

您需要确保选中Project> Build Automatically 。 否则它可能无法正常工作。

我可能会误解这个问题,但如果您在Eclipse中以调试模式(运行/调试)运行程序,则可以在程序运行期间编辑方法的内容(如果JVM支持它)。 通常,您无法更改导入,方法签名,类定义等,只能更改方法的内容。

启用Project-> Build Automatically后,调试模式下的热交换代码对我来说没问题