如何在调试模式下修改Java代码?

问题描述 投票:0回答:4

如何在 Eclipse 中启用 Notch 在此视频中讨论的“运行时调试”?

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

Eclipse 支持调试期间热插拔代码,开箱即用。

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

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

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


11
投票

您需要确保选中项目>自动构建。 否则可能无法工作。


2
投票

启用“项目”->“自动构建”后,在调试模式下热插拔代码对我来说是可以的


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.