Hot Code Replace problem.

Hello,

Up until I reformatted my computer and reinstalled Windows 8 on it, I have not been able to get Hotswapping working again. It worked fine before, but now it wont react at all. I am running a small segment of code to try it out;


public class GameComponent {
	
	public static void main(String[] args) {
		GameComponent gc = new GameComponent();
		gc.run();
	}

	public void run(){
		int x = 0;
		boolean running = true;
		while(running)
		{
			System.out.println("X = " + x);
			x = 2;
		}
	}
}

With this I should be able to run it in Debug mode and change the X value to print something else, but it does not seem to work. All my settings in Eclipse is default and I am having “Build Automatically” checked. Is there any more suggestions? Someone on LWJGL IRC said something about the build path but I think everything is OK there too.

Thanks,

That worked for me. How are you trying to change it? Are you setting a break point?

I am not setting any breakpoints, just running it in Debug mode and it starts printing the value of X, I am trying to change the value and saving but it does not change.

I checked the “Problems” window, but there is nothing there it seems. And I do not think I have some exclamation mark if the “Package Explorer” is what you mean.

By default, Eclipse changes entire methods, not individual lines of code. For a replaced method to become effective, it has to be called. This means that when multiple threads are accessing this method, each may be executing its own version of the method.

Is there a way to solve it?

Anyhow, figured out something interesting. I set a breakpoint on “System.out.println” and it stopped running the code when it hit it, so I ran it a few times getting the same value of X printed out. I changed the value and then continued to run it, breaking a few times at the breakpoint. This worked though, the X value was showing the changed value.

So I guess it only works if I hit the breakpoints though. Weird, but a clue.

You don’t necessarily need breakpoints. I’ve tested it, and while your code doesn’t work, this does (without breakpoints):

public class Test {
    private static int ctr = 0;
    public static void main(String[] args) {
        while (true) {
            method();
        }
    }

    private static void method() {
        System.out.println(ctr);
    }
}

You can change “ctr” to anything you want, and the change will reflect in the output. Adding breakpoints allows the JVM to “refresh” the stack frame. Here’s a quote that I think explains why your specific code doesn’t work:

[quote]There are some cases where the feature will not work. E.g. if you make changes to your main method’s a4 variable, the JVM will not be able to remove all stack frames running old code from the call stack. The debugger data will be lost.
[/quote]
Link

Your code works!

So it was my code all along. Thanks for your help, now I know that my settings in Eclipse are just fine.