LAB 11 - Debugging a java code

Goal

Debugging with java

Although a Java compiler always will detect syntax errors, often code that is syntactically correct is not logically correct. These sorts of problems may require the help of a debugger. Sun's Java Development Kit (JDK) includes a debugger called JDB (Java Debugger), a command-line application under which you can execute the programs you need to inspect. With JDB, you can inspect variables while a program is running, set breakpoints to halt the execution of a program at a certain point, and execute code line by line (known as single-stepping). You can see just how the contents of variables change after each statement of program code, and you can precisely trace the execution of a program--even as it branches to other functions or iterates through loops. JDB can be used to debug graphical programs and applets as well. An important thing to note, though, is that the programs you debug must be compiled with the -g flag. This causes the Java compiler to insert additional debugging information into the resulting Java class file.

A Debugging session with jdb

The -g flag tells the Java compiler to include additional information in the resulting .class file, which aids in the debugging process. Remember, to recompile your Java class without debugging information when releasing it for others to execute. That way, you minimize the file size of your classes. Plus, this does not adversely affect the execution of the class within the Java Virtual Machine in any way. There are many ways to start a jdb session. The most frequently used way is to have jdb launch a new Java Virtual Machine (VM) with the main class of the application to be debugged. This is done by substituting the command jdb for java in the command line. For example, if your application's main class is MyClass, you use the following command to debug it under JDB:

				  c:\> jdb MyClass 
				  

When started this way, jdb invokes a second Java VM with any specified parameters, loads the specified class, and stops the VM before executing that class's first instruction.

Breakpoints: The Most Useful Debugger Feature

The most common debugging technique is placing a breakpoint in a source line where you want to investigate either the state of variables or the visual appearance of the output screen. You might use this technique when you have an if statement that never seems to execute, for example. You might want to see the contents of the variables that are used in the if statement's condition and find out to what they evaluate. In fact, a breakpoint is the most useful feature in a debugger like JDB. It causes a running program to pause execution at the point you specify. You can see what the user interface looks like or examine any variables before resuming execution again. This feature is tremendously useful for understanding exactly what is happening within a program.

Breakpoint with jdb

Breakpoints can be set in jdb at line numbers or at the first instruction of a method, for example:

	 stop at MyClass:22 (sets a breakpoint at the first instruction for line 22 of the source file containing MyClass) 
				 

Stepping Through Code

Once execution of your program is paused, you have several ways of resuming execution of the code:

Stepping Through Code with jdb

The step commands advances execution to the next line whether it is in the current stack frame or a called method. The next command advances execution to the next line in the current stack frame.

Debugging with eclipse

The Eclipse Platform features a built-in Java debugger that provides all standard debugging functionality, including the ability to perform step execution, to set breakpoints and values, to inspect variables and values, and to suspend and resume threads.

How to start debugging with eclipse

Before you are able to debug your project, the code needs to compile and run cleanly. You first need to create a run configuration for your application and to make sure that it starts properly. Next, you need to set up the debug configuration in a similar way using the Run > Debug... menu. You also need to select the class to be used as the main Java class by the debugger). You can have as many debug configurations for a single project as you wish. When the debugger is started (from Run > Debug... ), it is opened in a new window, and you are ready to start debugging

Setting breakpoint in eclipse

When you launch your application for debugging, Eclipse automatically switches to the Debug perspective. Undoubtedly, the most common debugging procedure is to set breakpoints that will allow the inspection of variables and the values inside conditional statements or loops. To set a breakpoint, double-click in the gray margin on the left side of the editor . A blue dot will appear, indicating an active breakpoint. .

Stepping through code with Eclipse

In the title bar of the Debug view is a toolbar that lets you control t he program's execution. The first few tool buttons, which resemble the familiar controls of electronic devices such as CD players, allow you to resume, suspend, or terminate the program. Several buttons incorporate arrows in their design; these allow you to step through a program a line at a time. Holding the mouse over each button in turn will cause tool tips to appear, identifying them as Step With Filters, Step Into, Step Over, Step Return, and so on. For example, click the second step button, Step Into. Doing so executes the line of code that is currently highlighted in the editor area below the Debug view. Step Into, as the name suggests, takes you into the method that is called. The Step With Filters button works the same as Step Into, but it's selective about what methods it will step into. You normally want to step only into methods in your own classes and not into the standard Java packages or third-party packages. You can specify which methods Step Filter will execute and return from immediately by selecting Window.Preferences.Java.Debug.Step Filtering and defining step filters by checking the packages and classes listed there. Taking a moment to set these filters is well worth the trouble, because Step With Filters saves you from getting lost deep in unknown code—something that can happen all too often when you use Step Into.

Evaluating expression

To evaluate expressions in the editor in the Debug perspective, select the entire line where the breakpoint is set, and from the context menu, select the Inspect option. The expression is evaluated in the context of the current stack frame, and the results are displayed in the Expressions view of Display window

Viewing variables

The Variables view (in the Display window) displays the values of the variables in the selected stack frame. To view a requested variable, simply expand the tree in the Variables view until you can see the requested element. You can also watch variables in the Variables view as you step through the code in the Debug view.