Sign in to follow this  

Java Question: How do you do Hot Code Replace while debugging an application?

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Rewatching Ludum Dare livestreams of Java developers a lot of times, and trying to think up the method combination used to create the illusion of actually debugging an application and seeing different changes instantly, is pretty much what I've been doing for the past 2 months.

I think I have the concept down on a sheet of paper:

1. Type the code you want in Eclipse IDE.
2. Make sure you put a breakpoint in some lines where you want to make changes visually (for more on-the-spot debugging and visually controlling the elements and objects in the application you are debugging).
3. Debug the application.
4. When you hit the breakpoint, see if it is visually correct. (As in, see if the height of a cube is suppose to be accurate enough).
5. If not accurate enough, do [color="#ffa500"]"Hot Code Replace"[/color], something I didn't really see how it works in Eclipse.
6. [color="#2e8b57"]//DO SOME MAGIC HERE.[/color]
7. [color="#ff0000"]Repeat 4 through 7, if it's still not visually correct.[/color]
8. [color="#2e8b57"]//PROFIT!![/color]

What is the magic in Eclipse that I don't know of? :( I'm really looking forward to this. Where can I get the answer to this "debugging feature in Eclipse"?

And supposely, how do you visually see the changes you did in your code in a game application when debugging? Line by line execution is what I meant, in short.


UPDATE : Example Video:

Link to LiveStream: [b]http://tinyurl.com/4c8u9x5
[/b]Go to the timeframe 52:56. Watch how the developer instantly changes the object visually.

Share this post


Link to post
Share on other sites
Hot code replace is pretty much automatic. When you are debugging, change the code and save. The code is replaced in the running program, and if you allow execution to continue then it should re-render the next frame and stop you at the same breakpoint again.

There are some structural changes that cannot be replaced, but it will warn you in that case if you'd like to restart the program, stop it or just let it continue with the old code.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1298368025' post='4777463']
Hot code replace is pretty much automatic. When you are debugging, change the code and save. The code is replaced in the running program, and if you allow execution to continue then it should re-render the next frame and stop you at the same breakpoint again.[/quote]

Hm. How do you start debugging and be able to edit the code you make in Eclipse while you're in debugging mode? The instructions for doing a Hot Code Replace, or HotSwap, is vague. And the Eclipse interface is somewhat different from Microsoft Visual Studio, that I'm having a hard time understanding the features in Eclipse.

And is HotSwap (Hot Code Replace) perfectly okay if I change the string in println()? I can't ever get myself to do HotSwap. :(

[code]
public class mainClass
{
public static void main (String args)
{
while (true)
System.out.println("Do HotSwap in this line every time...");
}
}
[/code]

Share this post


Link to post
Share on other sites
There's a few ways to start debugging:

* Goto the "Run" menu at the top, then select "Debug" or "Debug as"
* Right click the java file that contains the entry point (main), and select "Debug as" from the pop-up menu.
* Use the little "bug" icon, by default it appears near the top.

Eclipse should switch to the debug perspective. You should see some extra windows/tabs, such as "variables" & "break points", aswell as all open source files.

If not, goto "Window -> Show View" and select them as required

Share this post


Link to post
Share on other sites
I was able to do it with the following program (writing it inside main() doesn't work because the current method gets "restarted" during hot replace):
[code]

class Help {

private static void printMessages() {
while(true) {
System.out.println("Hello, World!");
}
}

public static void main(String[] args) {
printMessages();
}
}
[/code]
Placing a breakpoint on the printing line, run using "Debug as..." for a few iterations and then go into the editor and change the string to something else. Save, and the code should be swapped (you don't need to do anything special). Loop a few more times to convince yourself it is working.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1298409882' post='4777703']
Placing a breakpoint on the printing line, run using "Debug as..." for a few iterations and then go into the editor and change the string to something else. Save, and the code should be swapped (you don't need to do anything special). Loop a few more times to convince yourself it is working.
[/quote]

So, it's:

1. Code.
2. Make a breakpoint somewhere where it's not in the main() method. (The line containing System.out.println() is where I put my breakpoint at)

or

2. Toggle method breakpoint.
3. Taskbar -> Run -> Debug As. -> Java Application
4. Press F8 multiple times...
5. Change some values.
6. Save.
7. Press F8 again to see the changes...
8. Repeat Step 5 through 8.

Right?

Share this post


Link to post
Share on other sites
According to the video, it was not shown pausing or pressing F8. The more I read your instructions, the more I get confused. Been looping the program and trying to change the string value, and saving after that, but it wouldn't hotswap the strings correctly...Unless it's possible to bind the whole steps into one hotkey...Or the steps shown above is the correct method of HotSwapping codes...

Just wanted a 100% clarification on the post above this one. Is it true? Or there are more to the HotSwap Code Replace method of debugging? Thanks in advance, thanks to rip-off for some of it, by the way.

Share this post


Link to post
Share on other sites
I got the following program to hot swap without breakpoints:
[code]

public class Help {

public static void run() {
System.out.println("Hello, World");
}

public static void main(String [] args) {
while(true) {
run();
}
}

}
[/code]
Putting an extra infinite while loop inside "run" caused the behaviour we saw earlier, not being able to swap the code.

Based on this, it appears the "hot swap" is implemented by replacing the function when it isn't active. If you are using a breakpoint it tries "drop to frame" to basically restart the function, which allows live editing the function.

The following program appeared to confirm this for me:
[code]

public class Help {

public static void run() {
long start = System.currentTimeMillis();
long current;
while((current = System.currentTimeMillis()) < start + 10000) {
System.out.println("Timer: " + ((current - start) / 1000));
}
}

public static void main(String [] args) {
while(true) {
run();
}
}

}

[/code]
If I edit the print statement it continues its current countdown. but it uses the new string the next time the function is called.

I hope this helps. I didn't know you could do hot swap with a breakpoint, I might find some use for this in the future.

Share this post


Link to post
Share on other sites
Oh my God. Thank you! You have helped me solve this problem I had trouble with since the start! Thank you! Here's a summary for others to read, just in case people didn't get it.

Summary:

HotSwap method:

1. To use, when coding, make sure the target function(s) you coded and wanted to debug in is separated from the main function that the program is going to execute in. This is to make the target function(s) you created to be able to pause (take a break, becomes inactive, etc.). This may turn out to be a good coding practice in Java.
2. After coding your code, go to the Taskbar -> Run -> Debug As... -> Java Application.
3. Observe the program to test and look for initial bugs. Fix them if found.
4. Change a value, or add additional codes to the current code.
5. Save (Ctrl + S). The Java Application should either instantly displays the changes, or continues executing its current function. Once the current function becomes inactive, it will induce "HotSwapping", and overrides the new changes to the current code. Using while() or for() makes it more obvious.

Share this post


Link to post
Share on other sites

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this