Eclipse Tutorial

< Part 4: Useful Eclipse Views

Part 5: Running and Debugging

Links >

Part 5: Running and Debugging

"In a few minutes a computer can make a mistake so great that it would have taken many men many months to equal it." Anonimous.

When your Java program is completed, it is time to execute and test it. Maybe some bugs will be found at execution time. Then, your program should be debugged. Fortunately, Eclipse provides additional support for running and debugging code.

 Executing

To run your program inside Eclipse, select "Run > Run..." from the top menu. Inside "Configurations" different executing configurations are stored. There are four kinds of running configurations: Java Applet (a web based application), Java Application (a standard application), JUnit (test cases) and Run-time Workbench (instances of Eclipse to test new Eclipse extensions).

So, to run a normal Java program, "Java Application" should be selected. Then click "Java Application" and press the "New" button to create a new running configuration. Inside the "Main" tab, name your running configuration, select the project which contains the Main class and select that Main class. Notice that a "Main class" is a Java class which contains a "main" method as a starting execution point. This main method is represented as a little running man decorator next to the class icon.

If you want to pass arguments to the main method (as "String[] args") just click the "Arguments" tab and write those arguments inside the "Program Arguments" area.

Finally, clicking the "Run" button will launch your selected program. A faster way to run the last executed configuration is clicking the running man icon from the top menu. Clicking the arrow next to this icon will show up other recently launched configurations.

Eclipse 3.0 Update

In Eclipse 3.0 that running man icon has been replaced by a white arrow inside a green circle.  

  Debugging

Besides Java is not as difficult to debug as other programming languages, some complicated execution problems may arise. Eclipse completely supports this debugging task through its "Debug" perspective ("Window > Open Perspective > Debug" or by selecting the "bug" icon from the left margin). Inside this debugging perspective, just by right clicking the code editor left margin a contextual menu will show up. Selecting "Add/Remove Breakpoint" will add or remove a breakpoint, while "Toggle Breakpoint" will change the breakpoint activation status. Breakpoints mark lines in which the execution of your program stops. So, it will be possible for the user to check variable values in order to discover possible bugs.

Right clicking a breakpoint and selecting "Breakpoint Properties..." will allow setting advanced breakpoint options. "Hit Count" specifies that the program execution will be stopped when passing through the breakpoint the selected number of times. The activation conditions will stop execution either when condition is true or when the condition value changes. Just specifying a variable as an activation condition and selecting "stop when condition changes" will be equivalent to "stop at the breakpoint when this variable is modified".

Exceptions are one of the most evident symptoms of execution errors. Java Exception Breakpoints stop execution whenever an exception of the selected type is thrown. These breakpoints are activated clicking the "J!" icon from the "Breakpoints View" or from the top menu "Run". Execution can be stopped when the exception is caught, uncaught or both. Adding always Java Exception Breakpoints for "ArrayIndexOutOfBoundsException" and "NullPointerException" is a recommended debugging practice.

A program should be run in debug mode ("Run > Debug...") if you want the execution to be stopped at the defined breakpoints. After stopping at a breakpoint, program execution can be continued. Right clicking the code editor window will open a pop up menu. "Run to line" resumes execution, letting the program run to the current line where the cursor is. "Step into selection" will continue execution inside the selected method, if the method source code is available. Execution can also be resumed right clicking the debug window, or pressing its icons. "Step over" will get you to the line after the current selected method. "Resume" will resume execution as normal, and will only be interrupted in the following breakpoints if their activation conditions were true.

The "Variables" view provides very useful information, as it shows the current variable values when execution is stopped at a breakpoint.

Also, the debug view is useful for watching different processes that are running simultaneously (i.e., threads). When the debugging process is finished, every process in the debug window show appear as finished ("Right click > Terminate"). Previous executions information can be removed just by right clicking on it and selecting "Terminate and Remove" or "Terminate All" plus "Remove All Terminated".

 Change Management

It is not rare changing program code and finding later that the new version works even worse than the first version. That is why programmers have to store different program versions every time they perform changes. Nevertheless, this unavoidable practice is often boring and time expensive. Fortunately, Eclipse provides a very powerful change management and version controlling feature. Right clicking a Java file in the Package Explorer View and selecting "Replace With > Local History" will let you replace your current version by a previously saved version. The modification date is shown, as well as two windows remarking the differences existent between both versions.

Selecting "Window > Preferences > Workbench > Local History" will allow you to select how many entries, MB, and days may be stored a program version. So, you can get a custom well balanced relationship between safety and efficiency.

< Part 4: Useful Eclipse Views

Part 5: Running and Debugging

Links >

Google
 
Web eclipsetutorial.forge.os4os.org

Return to index

A la versión española


This site is optimised for a 1024x768 screen resolution.

Created by Enrique Serrano Valle

This site contents are under the Creative Commons Attribution 2.0 license.

Eclipse trademark and logos appear according to Eclipse legal conditions. For more information check http://www.eclipse.org/legal/main.html

Java is a registered trademark of Sun Microsystems Inc.