# Core IDE - Part II - Debugging Java Applications

#### INTRODUCTION/OBJECTIVES

This workshop takes you through a demonstration of the WebSphere Studio Application Developer version 5.0 (hereafter called Application Developer) Core IDE. It focuses on the features in which a Java developer would be interested. The sample application is a simple on-line banking application represented by the Account project. We can withdraw funds from this account. This project has with two packages called com.ibm.demo.account and com.ibm.demo.account.testcases.

- Part I : Introduction to the Workbench for the Java Programmer
- Part II : Debugging Java applications
- Part III : Refactoring Java applications
- Part IV: ANT, JUnit and the Scrapbook in Application Developer

Note: This workshop requires that the following products are installed: WebSphere Studio Application Developer Early Availability Version 5.0 JavaTM 2 Software Development Kit, Enterprise Edition (J2EE SDK) 1.4

## Section 1 - Debugger Basics

___ 1. Setting Breakpoints

> a. If not already started, start Application Developer.
> b. If not already in a Java Perspective, open one by clicking on the Open Perspective button and selecting Java.
> c. If MyTestClient.java is not already open in the Java editor, double-click on MyTestClient.java in the Package Explorer view of the Java perspective.
> d. Set a breakpoint by positioning the mouse pointer directly to the left of the following line in the marker bar area and double-clicking. A blue circle appears, indicating a breakpoint.

>     CheckingAccount ca = new CheckingAccount(1,new BigDecimal(100.00));

> A breakpoint is a temporary marker placed in an executable program to tell the debugger to stop the program at a given point. Optional breakpoint parameters are used to control the behavior of breakpoints.
> e. Run MyTestClient in Debug mode by clicking on the Debug menu bar button.

> f. The Workbench switches to a Debug perspective automatically, as the title bar indicates. Take a moment to examine the Debug perspective. The program is stopped at the breakpoint, as indicated in the source view. It is executing on the built-in JRE. Program execution can be continued using various buttons in the Debug toolbar, such as the Step Into or Step Over buttons. Various views of variables, breakpoints and other important information are conveniently displayed in the upper right corner. The program console is displayed at the bottom of the perspective.
> g. Click the Step Over button until the program is stopped at the first call to the withdraw method. Click the Step Into button at this point to continue debugging in the withdraw method.
> h. Instead of being in the withdraw method, execution is halted in the BigDecimal method. This is because the call to the withdraw method contains a call to the constructor method for BigDecimal. Click the Step Return button to continue execution until execution returns from the BigDecimal method. This places execution back in the MyTestClient main method at the call to the withdraw, so click the Step Into button one more time.
> i. In the Variable view in the upper right corner of the Debug perspective, expand the this object to display the local variables currently being used. Select the acctNumber variable, click mouse-button 2 and select Change Variable Value. (You could also double-click the acctNumber variable). Change the acctNumber value to 2 and press Enter.

___ 2. Displaying and evaluating expressions

> a. Switch from the Variable view to the Display view by clicking on the Display tab.
> The Display view can evaluate the value of various Java expressions in the context current program execution.
> b. In the Display area, type howMuch, highlight it, click mouse-button 2 and select Display.
> This will display the value of the variable howMuch in the context of where program execution is currently halted. As shown, howMuch is a BigDecimal with a value of 10. If you see a different value, you are stopped in the different call to the withdraw method.
> c. The Display area has many of the same capabilities as the Java editor, such as Code Assist and Javadoc display. In the Display area, type this. and press Ctrl+space . Single click the getBalance method to display the Javadoc for the method.
> d. Double-click the getBalance method to finish the method call ( this.getBalance()). Highlight this method call, click mouse-button 2 and select Display. Note that the getBalance method is evaluated. If you see different values, you are stopped in the different call to the withdraw method.
> Click the Clear Display button (in the upper right hand corner of the Display view) to clear the Display view.

## Section 2 - Changing code

___ 1. Changing code in JDK 1.3

> a. In the Source code view of the Debug perspective, examine the code in CheckingAccount.java. After checking to make sure enough money is in the account to cover the withdrawal, the balance for the account is set by subtracting the withdrawal amount.
> After the call to setBalance, add the following code to subtract one additional unit from the account. It is commented so it can easily be found.

>     // Embezzlement
>     setBalance(getBalance().subtract(new BigDecimal(1)));

> The source view should look like this:
> b. Press the Ctrl+S key to save and compile this change. An error box appears indicating the target VM does not support hot code replace. As the code is running the default JDK 1.3 supplied with Application Developer, this is correct. Click OK to dismiss the error box. Note that the code changes ARE saved.
> c. Click the Resume button to finish running the application. Examine the Console view to verify that the embezzlement code did not execute by checking that the math is correct for the account balances and withdrawals.
> d. Close the Debug perspective by positioning the mouse pointer over the Debug icon on the Perspective bar, clicking mouse-button 2 and selecting Close.

___ 2. Changing code in JDK 1.4

> a. If not already open, open Checking Account.java in the Java editor in the Java perspective.
> Note that the code that was changed and saved in the Debug perspective is shown.
> b. Go back to the version of the code prior to adding the embezzlement code. To do this, select CheckingAccount.java in the Package Explorer view of the Java perspective, click mouse-button 2 and select Replace with->Local history... .
> c. In the Replace from Local History display, the top portion shows the various previous versions and dates for the file. In the lower portion, the current source is displayed on the left-hand side and the source of the currently selected previous version of the file is displayed on the right. The changes between the two versions are highlighted. In this case, there is only one previous version. Click the Replace button to replace the code in the Workbench with this previous version.
> The current version is replaced with the previous version, saved and compiled.
> d. Display the various JREs installed by clicking Window->Preferences on the menu bar. In the Preferences dialog on the left hand pane, expand Java and select Installed JREs. Note that both the default JRE 1.3 and the additional JRE 1.4 are available.
> Click Cancel to dismiss the Preferences dialog.
> e. In the Package Explorer view of the Java perspective, note the Account project is pointing to and using the rt.jar that is supplied with Application Developer.
> f. In the Package Explorer view of the Java perspective, select the Account project. Click mouse button 2 and select Properties.
> g. In the Properties dialog, select Java Build Path in the left hand pane and the Libraries tab in the right hand pane. Select JRE_LIB and click Remove.
> h. Click the Add Variable... button. In the New Variable Classpath Entry dialog, select JRE_14_LIB and click OK. This makes the runtime library for Account the JRE 1.4 library.
> Click OK to dismiss the Properties dialog. Note in the Package Explorer view of the Java perspective that the Account project is pointing to and using the rt.jar for JDK 1.4.
> i. Click the arrow next to the Debug button and select Debug... to display the Launch Configurations dialog.
> j. The Launch Configurations dialog shows the configured launch configurations. The Launch Configurations dialog allows you to configure different settings for launching your application. Select MyTestClient in the left hand pane and the JRE tab in the right hand pane. This shows that the MyTestClient launch configuration is using the default JRE that comes with Application Developer.
> Change the name of this launch configuration to MyTestClient - JRE 1.3 by appending ' - JRE 1.3' to the name. Click the Apply button.
> Examine the various other tabs available in the launch configuration dialog.
> k. In the Launch Configurations dialog, select Java Application in the left hand pane and click the New button to create a new launch configuration.
> l. Name this launch configuration MyTestClient - JRE 1.4. Click the Search... button to the right of the entry field for Main class and select com.ibm.demo.account.testcases.MyTestClient.
> m. Click on the JRE tab, click the drop-down arrow next to JRE and click JDK 1.4.
> n. Click on the Classpath tab and deselect Use default class path. Click on the Bootstrap classes subtab. Click the Advanced... button. In the Advanced Options dialog, select Add Variables. Click the OK button. In the Variable Selection dialog, select the JRE_14_LIB variable.
> Click the OK button.
> o. The Classpath tab, Bootstrap classes subtab page should look something like this:
> Click on the Apply button to save this new launch configuration.
> p. Still on the Launch Configurations dialog, click on the Debug button to start this launch configuration (using JDK 1.4) in Debug mode.
> q. A Debug perspective is opened and code execution is stopped at the previously set breakpoint. Note that the code is executing in the 1.4 JRE and using the rt.jar from that JRE.
> Use the Step Over, Step Into and Step Return buttons to again stop program execution at the first line in the withdraw method.
> r. Add the same two lines of code after the call to setBalance.

>     // Embezzlement
>     setBalance(getBalance().subtract(new BigDecimal(1)));

> The source view should look like this:
> s. Press the Ctrl+S key to save and compile this change. No error box appears as the 1.4 JRE supports hot method replace. Click the Resume button to continue program execution.
> t. In the console, prior to the "Insufficient Funds Exception thrown" message, note that an extra 1 unit per withdrawal is missing, indicating that the embezzlement code added during the debug session was executed during that same session.
> u. In the Variable view of the Debug perspective, click on the Breakpoints tab. Click on the Remove All Breakpoints button to get ready for the next part of the demonstration.
> Close the Debug perspective.

Congratulations, you've completed Part II, "Debugging Java applications" of the Core IDE lab and are ready to go to Part III, "Refactoring Java applications"!