DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 5.14 Changing Code on the Fly

5.14.1 Problem

You see a code error, and you want to fix it and test the fix, all without restarting the debugging session.

5.14.2 Solution

If you have Java Version 1.4 or later, just edit your code as you're debugging and save it (provided the auto-build on save capability is turned on), and you can keep debugging.

5.14.3 Discussion

The process of editing code while debugging is called hot code replacement, and you'll need Java Version 1.4 or later to use it. Make sure the "auto-build on save" capability is turned on (it's on by default). If it isn't, turn it on by selecting Window Preferences Workbench, and check the "Perform build automatically on resource modification" checkbox.

Now debug your code until execution stops at a breakpoint. At this point, you're free to edit your code. After editing, save the code and resume execution.

If you don't want to turn on the "auto-build on save" capability, simply rebuild your code manually after editing it by selecting Project Rebuild Project or Project Rebuild All, and then resume execution.


Note that you have to use Java Version 1.4 or later to support hot code replacement. If you're targeting an earlier version of Java with the actual code you're developing, in Eclipse you can use different JREs to run and debug your code. If you want to set the debug launch configuration so that it uses a different JRE, select Run Debug, then click the JRE tab and select the JRE you want to use for debugging.

5.14.4 See Also

Recipe 4.8 on selecting the Java runtime.

    Previous Section  < Day Day Up >  Next Section