Core IDE - Part I - Introduction to the Workbench for the Java Programmer

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 two packages called com.ibm.demo.account and com.ibm.demo.account.testcases.

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 - Setup

___ 1. Import files for the Account project

a. If not already started, start Application Developer.
b. Open a Java Perspective by clicking on the Open Perspective button



and selecting Java.

 CORE-0002.jpg
c. Create a new Java project by selecting File->New->Project. In the New Project wizard, choose Java from the left-hand pane and Java Project from the right-hand pane.

Click the Next> button.
d. In the Create a new Java project dialogue, specify a Project name of Account.

CORE-0003.jpg

Click Finish to create the Account Java project.
e. From the Package Explorer view in the Java Perspective, make sure Account is selected and click mouse-button 2. Select Import....

CORE-0004

f. From the Import selection dialog, select File system. Click the Next> button.
g. From the Import file system selection dialog, click the Browse... button. Navigate to and select the x:\WS50STEW\hands-on\CoreIDE\Account directory. Click Select All.

CORE-0005.jpg

Click the Finish button. Note that you will see a number of errors on the Tasks list. That is ok!

h. From the Package Explorer view in the Java Perspective, make sure Account is selected and click mouse-button 2. Select Properties.

i. In the Properties for Account dialog, select Java Build Path in the left-hand pane and the Libraries tab on the right -hand pane.

CORE-0006.jpg

j. Click Add External JARs.... Browse to and select  <APPDEV_ROOT>\eclipse\plugins\org.junit_3.7.0\junit.jar.   Note that <APPDEV_ROOT> is the directory where you installed Application Developer, such as D:\AppDevBeta.

CORE-0007.jpg

Click OK. This should remove the compilation errors.

k. If Application Developer is installed in D:\AppDevBeta, continue to 2. Configure the Workbench properties . If Application Developer is not installed in D:\AppDevBeta, continue with this step.

From the Package Explorer view in the Java Perspective, expand the Account project and look for build.xml. Double-click on build.xml to open it in the XML editor. Click on the Source tab to display the source view for this file. Locate the line:

classpath="d:\AppDevBeta\eclipse\plugins\org.junit_3.7.0\junit.jar"/>
Change the "d:\AppDevBeta "to the appropriate value for your installation.

___ 2. Configure the Workbench properties

a. This section will prepare the Workbench for various portions of the demonstration.  First setup the Workbench to use the J2EE 1.4 SDK.

From the Workbench, select Window->Preferences. Select Java->Classpath Variables from the left -hand pane to display the Classpath Variables. Click New....

CORE-0008.jpg

b. In the New Variable Entry dialog, enter JRE_14_LIB for the variable name and x:\JDKs\j2sdk1.4.0\jre\lib\rt.jar for the variable path. Click OK.

CORE-0009.jpg

c. With the Workbench Preferences dialog still open, select Java->Installed JREs from the left -hand pane to display the Installed JREs. Click Add....

CORE-0010.jpg

d. In the Add JRE dialog, enter JDK 1.4 for the JRE name. Click the Browse... button for the JRE home directory and browse to and select x:\JDKs\j2sdk1.4.0 . Click the Browse... button for the Javadoc URL: and browse to and select x:\JDKs\j2sdk1.4.0 (note that this is the place where src.jar is located for the JDK).

CORE-0011.jpg

Click OK.

e. Click OK to dismiss the Workbench Preferences dialog. Answer Yes to any questions about a full rebuild.

Section 2 - Exploring the Java perspective

___ 1. Explore the Java editor

a. If you are not already in a Java Perspective, open one by clicking the Open Perspective button and selecting Java. Note the Java Perspective, like all Perspectives, provides a customizable view to the components they are working on.

b. Expand and explore the com.ibm.demo.account and com.ibm.demo.account.testcases packages.

c. Double-click CheckingAccount.java to open it in the Java editor.

CORE-0012.jpg

d. Note the following: CORE-0013.jpg
___ 2. Explore the Type Hierarchy
a. In the Outline view of the Java Perspective, select the CheckingAccount class. Click mouse-button 2 and select Open Type Hierarchy.

CORE-0014.jpg

b. The Package Explorer view is replaced by a Hierarchy view. Note the following: CORE-0015.jpg
___ 3. Explore the Build Path
a. Switch back to the Package Explorer view by clicking on the Package Explorer tab.

CORE-0016.jpg

b. Select the Account project, click mouse-button 2 and select Properties to display the properties of the Account project.

c. Select Java Build Path in the left-hand pane to display the properties of the Java build path for the Account project. The Order and Export tab is displayed. Click on the Libraries tab. Note that if we need to add external JAR files we would click on the Add External JARs ... button. We have two JARs configured in the build path, the JRE_LIB - D:\AppDevBeta\eclipse\plugins\org.junit_3.7.0\ junit.jar file and the JRE_LIB - D:\AppDevBeta\eclipse\jre\lib\rt.jar file for JDK 1.3.

Note: The Add JARs... button will add JAR files that exist in the workspace to the build path for this project. The Add External Jars... button will add JARs files that do not exist in the workspace to the build path for this project.

CORE-0017.jpg

Click the Cancel button to dismiss the Properties dialog.

d. Note the following about the Package Explorer view:
CORE-0018.jpg


Section 3 - Java perspective basics

___ 1. Working with classes
a. In the Package Explorer view of the Java Perspective, double-click on MyTestClient.java (in the com.ibm.demo.account.testcases to open it in the Java editor. In order to switch between the two different open classes (CheckingAccount.java and MyTestClient.java) simply select the appropriate title tab.

CORE-0019.jpg

Double-click on the title tab for MyTestClient.java to cause the Java editor to fill the entire Workbench view.

b. Examine the code for MyTestClient.java. Briefly, it:
Double-click on the title tab for MyTestClient.java again to restore the Java editor to it's default state. Close CheckingAccount.java by clicking on the X in the title tab for CheckingAccount.java.

CORE-0046.jpg
___ 2. Working with Javadoc
a. In MyTestClient.java, move your cursor over the BigDecimal constructor to display the Javadoc for BigDecimal.

CORE-0020.jpg

Do the same for System.

Note that if there are methods or objects without Javadoc, all that will be displayed will be the method signature. Verify this by moving your cursor over the withdraw method.

CORE-0021.jpg

This can be turned on or off by clicking the Show Text Hover button.

CORE-0022.jpg

b. You can look at the Javadoc for a class by selecting the class and clicking Navigate->Open External Javadoc. The keyboard shortcut for this is Shift+F2 .

CORE-0023.jpg

From the Outline view, select MyTestClient. From the Workbench menu, click Navigate->Open External Javadoc. You will see the following message:

CORE-0024.jpg

This is because we have not configured the location for the Javadoc nor have we generated the Javadoc.

c. First, verify the location of the Javadoc command. From the Workbench menu, click Window->Preferences. Expand Java on the left-hand pane and select Javadoc. Click the Browse... button and browse to and select <APPDEV_ROOT>\runtimes\aes_v4\java\bin\javadoc.exe.


Note that <APPDEV_ROOT> is the directory where you installed Application Developer, such as D:\AppDevBeta.

CORE-0025.jpg


Click the OK button.

d. Now create the Javadoc for the Account project. In the Package Explorer view, select the Account project. Click mouse-button 2 and select Export....

e. In the Export Select dialog, select Javadoc. Click Next>.

f. In the Generate Javadoc dialog, ensure that Account is selected. Click Finish to generate the Javadoc for the Account project.

On the pop-up message asking if you want to update the Javadoc location, click Yes.

Ensure that the console displays messages indicating that Javadoc is being generated. If you are on a slower machine, it may take a few seconds.

CORE-0048.jpg

NOTE: If the console does not display any messages, from the Workbench menu click Window->Show View->Debug.  In the Debug view, select the Javadoc Generation process.

CORE-0049.jpg

The click the Console tab to display the console.

g. Now display the Javadoc for the MyTestClient. Select MyTestClient. From the Workbench menu, click Navigate->Open External Javadoc. You should see the Javadoc displayed for MyTestClient.

CORE-0026.jpg

h. Close the Javadoc window.
___ 3. Working with Code Assist and Templates
a. In the Java editor for MyTestClient.java, add a new line after the try { line.

CORE-0027.jpg

b. Type System.out. and press Ctrl+Space Bar to activate Code Assist. Code Assist is also activated after a configurable period of time. Code Assist displays a list of available methods or objects based on where the cursor is in the source editor. It also displays the Javadoc for that method or object.

CORE-0028.jpg

Finish the line of code so it appears as follows and add another blank line after it.:
System.out.println("Hello");
c. Code Assist also works with Java language elements. Type if and press Ctrl+Space Bar to activate Code Assist and display various if statement templates as well as potential class name matches. A template is a convenience for the programmer to quickly insert often reoccurring source code patterns.

CORE-0029.jpg

d. Select the ifelse statement template choice. Note that the template fills in multiple lines and contains hints for what type of language construct is needed.

CORE-0030.jpg

Remove the if statement you just added by pressing Ctrl-Z twice.

f. Define a new template for System.out.println(); by clicking on Window->Preferences from the Workbench menu bar. In the left-hand pane, expand Java and select Templates. This is a list of all the Templates defined in the Workbench.

CORE-0031.jpg

Click the New... button.

g. In the New Template dialog, enter sop for Name: and System.out.println for the Description.

Enter System.out.println(); for pattern and with the cursor between the parenthesis, click the Insert Variable... button. Double click cursor.

CORE-0032.jpg

You should see:

CORE-0033.jpg

Click OK to save the sop template. Click OK to dismiss the Preferences dialog.

h. In the Java editor for MyTestClient.java, put the cursor in the blank line after System.out.println("Hello"); .

Type sop and press Ctrl+Space Bar. You should see Content Assist fill in the line with System.out.println(); and position your cursor between the parenthesis.
___ 4. Adding Getters and Setters
a. In the Java editor for MyTestClient.java , add a blank line after the Class declaration, that is after:
public class MyTestClient
b. Add a new variable by typing:
private int i;
Note the Outline view is updated with the variable when you complete the statement.

CORE-0034.jpg

c. Generate getters and setters for the variable i that was just added by selecting i in the Outline view, clicking mouse button 2 and selecting Generate Getter and Setter....

CORE-0035.jpg

d. On the Generate Getter and Setter dialog click OK to create the getter and setter methods.

CORE-0036.jpg
___ 5. Running methods with errors
a. In the Java editor for MyTestClient.java, move the cursor to a blank line right after the main method and just before the comments for the getI method.

CORE-0037.jpg

b. Add a new method by typing:
public void errorMethod()
{
System.out.printlx();
}
Note the this is an invalid line of code. That is ok!!!

c. Save and compile this class by press the Ctrl+S key. Note that an error will show up in the Task list because of this bad line of code. You can also move your mouse pointer over the red x in the Task list or in the Java editor (where the red x might be hidden by a lightbulb) to display Hover help about the error.

CORE-0039.jpg

d. In other Java IDEs, the MyTestClient class could not be executed because it has errors. In Application Developer, we can run this class as long as we do not try to execute the method in error. The main method does not call errorMethod, so we can execute this class. Click on the Run button.

CORE-0040.jpg

e. On the Launch Configurations dialog, ensure Java Application is selected and click the New button.

f. On the Launch Configurations dialog, ensure MyTestClient is selected on the left-hand pane and click Run.

You should see the output from MyTestClient in the console.

CORE-0041.jpg

___ 6. Adding try/catch blocks
a. In the Java editor, remove the try/catch block from MyTestClient by removing:
try {
and
} catch(InsufficientFundsException e) {
System.out.println("Insufficient Funds Exception");
}
b. Save and compile this class by pressing the Ctrl+S key. Note that errors appear in the Task list because MyTestClient no longer handles the InsufficientFundsException exception.

c. Highlight the code formerly surrounded by the try/catch block by putting the cursor at the start of the System.out.println("Hello"); statement, pressing and holding the Shift key and moving the cursor by using the down arrow key until the cursor is on the System.out.println("Withdrawing 100"); statement.

CORE-0042.jpg

d. Press mouse button 2 and select Source->Surround with try/catch block.

CORE-0043.jpg

Note that the code is surrounded by a try/catch block and catches the appropriate exception.

e. In the catch block between { and } enter the following highlighted code (remember to use the sop template!)
System.out.println("Insufficient Funds Exception thrown");
CORE-0047.jpg

f. Save and compile this class by pressing the Ctrl+S key. Note that errors related to the InsufficientFundsException exception no longer appear.
___ 7. Using Quick Fixes
a. Quick Fixes provide suggestions as to how to fix compile errors. In the Java editor, find the red box on the right hand side of the scroll bar (indicating a compile error) and press the box to move the editor to that line and error.

CORE-0044.jpg

b. Note the lightbulb icon to the left of the line in error. This means a Quick Fix is available. With the error highlighted, press Ctrl+1 (the number one) to activate Quick Fix. Note that a suggested fix is presented. Choose this fix by double-clicking on it.

CORE-0045.jpg

The change is made in the source code.

c. Save and compile this class by pressing the Ctrl+S key. Note that the error related to the System.out.printlx problem no longer appears.

d. Quick Fixes can also be used to quickly create a method. In the Java editor, add a new line below the just corrected System.out.println();. Enter the following line of code:
trace();
and press the Ctrl+S key. Note that a compile error relating to this undefined method trace is displayed.

e. With the error selected in the Java editor, press Ctrl+1 (the number one) OR click Edit->Quick Fix from the Menu bar. Either way, Quick Fix displays a suggested fix of creating the trace() method. Chose this fix by double-clicking on it. A new method called trace() is created.

f. Save and compile this class by pressing the Ctrl+S key. Note that the error related to the missing trace() method no longer appears.
Congratulations, you've completed Part I, "Introduction to the Workbench for the Java Programmer" of the Core IDE lab and are ready to go to Part II, "Debugging Java applications"!