Changing Code on the Fly

Eclipse blurs the line between editing and debugging by letting you modify a running program. You don't have to stop the program—just edit and save it. If possible, Eclipse will compile just the class that was modified and insert it into the running process. This handy feature is called hot code replace.

Tip

If you modify a method that the program is currently executing, the debugger will have to drop to the previous frame and begin that method again from its first line. This doesn't work on the main() method because there is no caller.

Some kinds of changes can be made on the fly and some cannot. Simple things (like fixing an expression formula, changing comments, adding new local variables, adding new statements to an existing method, etc.) should work fine. If for some reason execution cannot continue, you will get an error dialog with the option to continue without making the change, terminate the program, or terminate and restart it from the beginning.

Tip

Hot code replace requires special support from the Java virtual machine that is not present in all versions of Java. It's known to work in Sun's Java Version 1.4.2 and later, but not all vendors support it. If your Java version does not support it, you'll get an error dialog when you try to save.

The debugger has so many features that it's impossible to cover them all here. Part VI covers more advanced topics that impact running and debugging your program, especially in the "Launch Configurations" section. But in your first pass through this book, you may want to continue with Part V, which covers unit testing. Later, you can go to Part VII to find out what all those buttons in the Debug and Breakpoint views do.

The Eclipse online help is also a good resource for information on running and debugging. See the following sections in the User's Guide (Help → Help Contents → Java Development User Guide):

  • Concepts → Debugger

  • Tasks → Running and debugging

Get Eclipse IDE Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.