Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在调试模式下运行时修改Java代码?_Java_Eclipse_Debugging - Fatal编程技术网

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

如何在调试模式下运行时修改Java代码?,java,eclipse,debugging,Java,Eclipse,Debugging,如何启用Eclipse中提到的“运行时调试”缺口 作为测试,我希望能够编辑以下代码的输出,并在其运行时将其更改为“Hello Runtime Debugging” public class HelloWorld { public static void main(String[] args) throws InterruptedException { doIt(); } private static void doIt() throws Int

如何启用Eclipse中提到的“运行时调试”缺口

作为测试,我希望能够编辑以下代码的输出,并在其运行时将其更改为“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);
        }
    }
}
公共类HelloWorld{
公共静态void main(字符串[]args)引发InterruptedException{
doIt();
}
私有静态void doIt()抛出InterruptedException{
对于(int i=0;i<1000;++i){
System.out.println(“你好世界”+i);
Thread.currentThread().sleep(100);
}
}
}

编辑:我修改了代码,现在得到了我想要的结果。苏拉杰·钱德兰在下面的回答解释了这一点

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);
}
private static void doIt()抛出InterruptedException{
对于(int i=0;i<1000;++i){
印刷品(一);
Thread.currentThread().sleep(100);
}
}
专用静态无效打印(int i){
System.out.println(“你好先生”+i);
}

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

Eclipse支持在调试过程中即时热交换代码

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

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


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

您需要确保选中“项目>自动生成”。
否则它可能无法工作。

在启用Project->Build Automatically之后,在调试模式下热插拔代码对我来说是可以的

你怎么做?我放了一个断点,假设循环计数为23,它将变成“Hello World 22”“Hello World 23”我将文本更改为其他内容,我按resume,现在我得到“Hello其他内容0”它会重新启动程序吗?计数突然为0。如果重新启动程序,它将从一开始重新执行当前方法。因此,它将重新设置所有变量以解释它。你介意把它写在答案里吗?