sun one integrated development...

65
© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved. 5 Sun ONE Integrated Development Environment Objectives To be able to use Sun ONE to create, compile and execute Java applications and applets. To understand and be able to use Sun ONE’s integrated development environment. To be able to use the online documentation. To be able to use the debugger to locate program logic errors. DiveIntoSunONE.fm Page 197 Tuesday, September 24, 2002 8:49 AM

Upload: others

Post on 26-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5Sun ONE Integrated

Development Environment

Objectives• To be able to use Sun ONE to create, compile and

execute Java applications and applets.• To understand and be able to use Sun ONE’s

integrated development environment.• To be able to use the online documentation.• To be able to use the debugger to locate program logic

errors.

DiveIntoSunONE.fm Page 197 Tuesday, September 24, 2002 8:49 AM

Page 2: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

198 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5.1 IntroductionWelcome to the Sun ONE integrated development environment. In this chapter you willlearn how to create, compile, execute and debug Java programs using the powerful Java de-velopment environment from Sun Microsystems—Sun ONE Studio 4, Community Edi-tion. This product was formerly called Forte for Java 4, Community Edition, but SunMicrosystems changed the name of the product. However, it is still the same basic product.In this guide the IDE will be referred to as Sun ONE Studio 4, Community Edition. Whenyou complete this chapter, you will be able to use Sun ONE to begin building applets andapplications.

Outline

5.1 Introduction5.2 Getting Started with the Java 2 Software Development Kit 1.4

5.2.1 Installation of Java 2 SDK5.2.2 Compiling and Executing an Application5.2.3 Compiling and Executing an Applet

5.3 Installing Sun ONE Studio 4, Community Edition5.4 Starting Sun ONE Studio 4, Community Edition for the First Time5.5 IDE Overview5.6 Online Help5.7 Compiling and Executing Applications and Applets

5.7.1 A Java Application5.7.2 A Java Applet

5.8 Creating and Compiling Applications and Applets5.8.1 Creating a New Application5.8.2 Creating an New Applet

5.9 Using the New Form Template Wizard

5.9.1 Creating an Application5.9.2 Creating an Applet

5.10 Using Packages5.11 The Sun ONE Debugger

5.11.1 Debugging a Java Application5.11.2 Debugging a Java Applet

DiveIntoSunONE.fm Page 198 Tuesday, September 24, 2002 8:49 AM

Page 3: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 199

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5.2 Getting Started with the Java 2 Software Development Kit 1.4Before we begin creating and executing Java applications with Sun ONE Studio 4, Commu-nity Edition, the software must be installed in conjunction with Java 2 Software Develop-ment Kit (J2SDK). The J2SDK is on the CD-ROM that accompanies the Java How ToProgram textbook, and Sun ONE can be downloaded from Sun’s Web site. This sectionprovides an overview of the installation procedure.

5.2.1 Installation of Java 2 SDKJava 2 Software Development Kit is the Virtual Machine that executes Java applications.To ensure proper interaction between Sun ONE and J2SDK, J2SDK must be installed first.[Note:Sun ONE 4 requires the use of version 1.3.1 of J2SDK or higher. Sun ONE will notinstall if a lower version than 1.3.1 of J2SDK is used.]

1. The Java 2 SDK installation executable is provided on the CD-ROM and is alsoavailable for download from Sun’s site, java.sun.com/j2se/1.4/down-load.html. This site also gives the user the option to download a cobundle thatcontains both the J2SDK and Sun ONE Studio 4, Community Edition installationspackaged into a single executable file. To begin the installation from the CD-ROM, click the Start button and choose the Run… option. In the Open field,also referred to as the command line, type D:\software-win\sj-sdk\j2sdk-1_4_0-win.exe, assuming D: represents the CD-ROM drive.If the executable was downloaded, simply enter its location on the hard drive inthe Open field and click OK.

2. The installation begins by extracting necessary files. Wait for the installation toproceed until it reaches the welcome screen (Fig. 5.2) for the InstallShield Wiz-ard. When ready to continue, click the button on the welcome screen labeledNext. The next window presents the license agreement. The user must agree tothe terms of the license agreement to continue installation. It is recommended thatthe user carefully read the agreement, and when willing, click the button labeledYes. If the user disagrees with the license agreement, clicking the No button willallow the user to exit the installation without modification to the computer. It isalso important to note that the Back button is available from this point forward,

Fig. 5.1 The Run dialog under the Windows Start menu.

DiveIntoSunONE.fm Page 199 Tuesday, September 24, 2002 8:49 AM

Page 4: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

200 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

which allows the user to navigate the installation sequence in reverse order. Thisis useful to correct mistakes or make changes.

3. In the next window (Fig. 5.3 ) the user selects the destination of the software. Thedefault directory is C:\j2sdk1.4.0 (where C: represents the appropriate diskdrive). It is recommended the user accept the default option because J2SDK is aplatform that interacts with many separate software packages. The default folderis specific and easy for other Java related programs to detect. However, to changethe destination folder click the Browse button, which displays a dialog boxthrough which the user can either visually navigate the directory structure or cre-

Fig. 5.2 InstallShield Wizard welcome screen.

DiveIntoSunONE.fm Page 200 Tuesday, September 24, 2002 8:49 AM

Page 5: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 201

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

ate a new destination folder. Once the desired destination is selected, click Next>.

4. The next window allows the user to select which components of J2SDK to install.The bottom-left of the window shows the total space available on the appropriatedrive as well as the space that is required for installation, which is dependent uponwhat components are selected for installation. To accept the default configurationand install all of the components, click Next > and continue to the next step. Tocustomize the installation, select or remove components through their associatedcheckboxes. For information about a specific component click its name. A briefdescription will appear on the right side of the window. When the desired compo-nents are selected, click Next >.

5. The next window is the Select Browsers window, where the user selects whichWeb browsers will use Java Plug-in as its default Java runtime. Java Plug-in is abrowser runtime that enables the execution of Java applets. To change the default

Fig. 5.3 Choose Destination Location window.

DiveIntoSunONE.fm Page 201 Tuesday, September 24, 2002 8:49 AM

Page 6: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

202 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

Java runtime for a particular browser, simply uncheck it. Click Next > whenready to continue.

6. Next, the appropriate files are copied to the destination folder. The Setup Statuswindow presents the status of the installation as the files are copied. When com-plete, the InstallShield Wizard Complete window is presented. From thiswindow, the user can decide whether to view the README file, which provides in-formation about new features and how to setup the environment. Click Finish to

Fig. 5.4 Select browsers for Java Plug-in.

DiveIntoSunONE.fm Page 202 Tuesday, September 24, 2002 8:49 AM

Page 7: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 203

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

complete the installation. Java 2 Software Development Kit has been successfullyinstalled.

5.2.2 Compiling and Executing an Application

The following section is designed to walk through the process of compiling and executingany of the programs that can be found within the Java How to Program, Fourth Editionbook without using Sun ONE. First the user must copy a version of Fig. 2.1 and place it ontheir local hard drive. That is the program used in this case. The file can be obtained by us-ing the CD that is located in the back of the Java book. All exercises that are within the JavaHow to Program, Fourth Edition textbook can be found on this CD. The user can accessthese examples by clicking the Examples button, then opening the appropriate chapter,and then opening the correct figure number. In this case the file is located in the ch02 fold-er and within that, in the Fig02_01 folder. Copy the files within the folder to the local harddrive. Files on the CD are "Read Only." To compile the programs correctly, this propertymust be changed. To accomplish this, right click on the file and select the Properties itemfrom the list. In the attributes section, click the Read Only checkbox to change this option.

1. In order to compile a file without Sun ONE installed, the Javac.exe file, pro-vided with the Java 2 SDK, must be used. The program must be run through a con-sole window, also referred to as the command prompt. To access the command

Fig. 5.5 Installation complete.

DiveIntoSunONE.fm Page 203 Tuesday, September 24, 2002 8:49 AM

Page 8: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

204 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

prompt choose Run... from the Start menu and enter cmd. This will bring up anempty console window.

2. The file, Javac.exe, is located in C:\j2sdk1.4.0\bin, provided that C:\is the local hard drive in which the Java 2 SDK is installed. To run it in the com-mand prompt just enter in javac. This will bring up a help list to provide the userwith more advanced options (Fig. 5.7).

3. In order to run the program enter into the command prompt javacWelcome1.java. Note that the java compiler is case sensitive so that entering aname like javac welcome1.java will generate an error. This will compile theChapter 2 Figure 2.1 file, Welcome1.java.

4. The Java.exe file, located in C:\j2sdk1.4.0\bin, is used to run applica-tions. To run it in the command prompt enter in java. This will also bring up ahelp list to provide the user with more advanced options (Fig. 5.8).

Fig. 5.6 Running the Command Prompt.

Fig. 5.7 javac.exe help menu

DiveIntoSunONE.fm Page 204 Tuesday, September 24, 2002 8:49 AM

Page 9: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 205

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5. In order to run the program, enter into the command prompt java Welcome1.This executes the file Welcome1.class that was created by compilingWelcome1.java. The output is then displayed in the console window(Fig. 5.9).

5.2.3 Compiling and Executing an Applet

Compiling and executing an applet is similar, though not exactly the same as executing anapplication. Applets are designed to run in a web browser which is not the traditional waya program is run. The Java 2 SDK provides an Appletviewer program that allows an appletto be run. Appletviewer.exe is located in C:\j2sdk1.4.0\bin with java.exe.

Fig. 5.8 java.exe help menu.

Fig. 5.9 Welcome1 output.

DiveIntoSunONE.fm Page 205 Tuesday, September 24, 2002 8:49 AM

Page 10: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

206 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

Again, first copy the files from the fig03_06 folder in the ch03 folder within the Exer-cises button on the Java CD to the local hard drive.

1. Appletviewer.exe, like java.exe, must be executed from the consolewindow.

2. In order to run the appletviewer its directory must be mapped as a system variable.This is done by entering set PATH=%PATH%;C:\j2SDK1.4.0\bin into thecommand prompt. This assumes C:\j2SDK1.4.0 is the directory where theJava 2 SDK is installed. This will enable appletviewer to be typed in any direc-tory and allow it to be executed. [Note: the path has to be changed each time thecommand prompt is opened because it is not permanently saved.]

3. Java applets must also be compiled before they can be run. Applets are compiledin the same manner as applications. For information on compiling, seeSection 5.2.2.

4. Entering appletviewer in the command line will bring up the help menu formore advanced executions. This can be done to help the viewer find out moreabout the debugger or other help topics (Fig. 5.11).

5. To run the applet type appletviewer WelcomeApplet.html. This will compilethe applet and display the programs results. The program output will be displayed

Fig. 5.10 Creating a system variable.

Fig. 5.11 appletviewer.exe help menu.

DiveIntoSunONE.fm Page 206 Tuesday, September 24, 2002 8:49 AM

Page 11: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 207

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

in a separate window (Fig. 5.12). When closed the window will terminate the ap-plet.

6. The applet can also be run by loading the HTML document with either InternetExplorer (Fig. 5.13) or Netscape Navigator. The file provided contains text to runthe applet. To close the applet simply close the web browser.

Fig. 5.12 WelcomeApplet.java output.

Fig. 5.13 Running WelcomeApplet.java through Internet Explorer 6.0.

Fig. 5.14 Running WelcomeApplet.java through Netscape Navigator 6.2.

DiveIntoSunONE.fm Page 207 Tuesday, September 24, 2002 8:49 AM

Page 12: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

208 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5.3 Installing Sun ONE Studio 4, Community EditionOnce J2SDK is installed on your machine, you are capable of executing Java applicationsand applets. To develop these applications, we now install the Sun ONE development en-vironment. Sun ONE software is separate from J2SDK, but it utilizes J2SDK to executeprograms that we develop with Sun ONE. [Note: J2SDK must be installed before Sun ONEcan be installed.]

1. The Sun ONE installation executable is available for download from Sun's site,wwws.sun.com/software/Developer-products/ffj/buy.html.Download and Save the file to a known place on the local hard drive or simplyclick Open to execute the file after the download is complete.

2. The setup file can be run by clicking Start and selecting Run… from the menu.Then enter the location of the setup file into the Open field (Fig. 5.15), or clickthe Browse button to select the setup file.

3. The installation begins by extracting necessary files. Wait for the installation toproceed until it reaches the welcome screen for the InstallShield Wizard. Whenready to continue, click the button on the welcome screen labeled Next >.

4. The next window presents the license agreement. The user must agree to the termsof the license agreement to continue installation. It is recommended that the usercarefully read the agreement, and when willing, select I accept the terms ofthe license agreement and click Next >. If the user disagrees with the licenseagreement, selecting I donot accept the the terms of the agreement andclicking Cancel will allow the user to exit the installation without modificationto the computer. It is also important to note that the Back button is available fromthis point forward, which allows the user to navigate the installation sequence inreverse order. This is useful to correct mistakes or make changes.

5. Next, the installation searches the system for a Java Virtual Machine (JVM),which is required to run Sun ONE. If J2SDK is not installed, stop trying to install

Fig. 5.15 The Run dialog under the Windows Start menu.

DiveIntoSunONE.fm Page 208 Tuesday, September 24, 2002 8:49 AM

Page 13: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 209

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

Sun ONE. J2SDK must be installed before Sun ONE can be installed. Click Nextto continue.

6. The next screen allows the user to select the folder where Sun ONE will be in-stalled to. The default directory is C:\forte4j. To modify the destination fold-er, click Browse and select a new location. When completed, click Next > tocontinue. If there are files from a previous version of the IDE on the computer,Sun ONE backs them up by moving them to ffj_backup folder.

Fig. 5.16 Find a Java virtual machine.

DiveIntoSunONE.fm Page 209 Tuesday, September 24, 2002 8:49 AM

Page 14: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

210 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

7. The installation will then display the component selection dialog. Components areselected for installation via their associated checkboxes. To the right of each com-ponent is the space it requires for installation. When completed, click Next to con-tinue.

Fig. 5.17 Selecting the installation directory.

Fig. 5.18 Select Sun ONE components to install.

DiveIntoSunONE.fm Page 210 Tuesday, September 24, 2002 8:49 AM

Page 15: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 211

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

8. The next window allows the user to associate file extensions (i.e., .exe for an ex-ecutable) with Sun ONE. Associating the Java and NetBeans file extensions en-ables the user to open those files by double-clicking when the Sun ONE IDE isrunning. If the user wishes not to associate file extensions, the files can still beopened through the Sun ONE IDE.

9. The next window reviews the installation settings to this point. Verify that all ofthe settings are correct and when ready to begin copying files, click Next. Whencopying is complete, the installation will display the InstallShield WizardComplete window. To exit the installation, click Finish.

Once J2SDK and Sun ONE Studio 4, Community Edition are successfully installed,you have everything you need to begin to develop and execute your own Java programs.

5.4 Starting Sun ONE Studio 4, Community Edition for the First TimeSun ONE can be opened by either double-clicking on the icon placed on the desktop or byselecting the Forte for Java item under the Start > Program Files > Forte for Java4 CE menu. When Sun ONE opens for the first time it will present the user with three op-tions.

1. The first option allows the user to specify the directory where the individual fileswill be stored. This directory can be created while Sun ONE waits. The createddirectory can then be entered into the textbox provided by Sun ONE.

2. The second option asks where previous versions of Sun ONE have been installed.The Sun ONE installation program assumes that a previous version of Sun ONEhas been installed on the computer, and asks where it is, so the new version canuse the same settings as the old version. Skip this. The installer will say that thesun.tools.debugger will not be able to used. This is not a problem, sincethe standard debugger provided by Sun ONE is sufficient for debugging applica-tions.

3. The third option asks the user to register Sun ONE Studio 4, Community Edition.If you wish to register now, select Register Using Web or Register by Fax,and click Next >. Follow the registration wizard to complete the registration. Ifyou do not wish to register at this time, select Register Later or Never Regis-ter and click Finish to exit the registration screen.

5.5 IDE OverviewFigure 5.18 shows the initial screen image of the Sun ONE integrated development envi-ronment (IDE). This environment contains everything you need to create Java programs—an editor (for typing and correcting your Java programs), a compiler (for translating yourJava programs into Java bytecode), a debugger (for finding logic errors in your Java pro-grams after they are compiled) and much more. The environment contains many buttons,

DiveIntoSunONE.fm Page 211 Tuesday, September 24, 2002 8:49 AM

Page 16: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

212 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

menus and other graphical user interface (GUI) elements you will use while editing, com-piling and debugging your Java applications.

5.6 Online HelpSun ONE offers an interactive help menu that allows the user to search through any in-stalled documentation. Sun ONE help offers guides and tutorials as well as any related top-ics to the selected document. There are even documents found within the help index thatprovide pieces of code that are used to invoke certain methods and help the user get a feelfor a specific class. The main purpose of the help however is for use with the Sun ONE IDEand support for other modules and schemas. There are three different ways to find a desireddocument, all of which are outlined in the section below. There is also an online help thatis accessible through the internet. The site is found at servlet.java.sun.com/help.

Fig. 5.19 Sun ONE initial screen.

DiveIntoSunONE.fm Page 212 Tuesday, September 24, 2002 8:49 AM

Page 17: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 213

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

1. Going to the Help menu and selecting Contents can open the online help. Press-ing the F1 key can also be used to access it.

2. First off, each help topic can be browsed individually. This can be done by goingto the Help menu and choosing the Help Sets submenu. From there the user canselect any one of the major groups of help topics. This allows the user to view helpdocuments on only the desired topic.

3. The help topics can be browsed in one of three ways. They can be organized bytopic, they can be organized alphabetically, or they can be searched. Using theContents tab, the Index tab, and the Search tab, respectively, can access thesedifferent browsing techniques. There are two arrows at the top of the Help screen.

Fig. 5.20 Help menu in Sun ONE.

Fig. 5.21 The Help Sets menu.

DiveIntoSunONE.fm Page 213 Tuesday, September 24, 2002 8:49 AM

Page 18: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

214 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

They are used to navigate through the Help topics much like a web browser. Thearrow pointing to the left is used to go back and the arrow pointing to the right isused to go forward. The Printer icon is used to print the screen and the Printericon with an O on the bottom right corner is used to enter the Page Setup win-dow.

4. The Contents tab, which has the picture of a book on it, is used to look througha list of topics. Each topic is represented by a folder which may also contain subfolders. Within these folders are documents that contain information about the

Fig. 5.22 Help icons for navigating and printing.

DiveIntoSunONE.fm Page 214 Tuesday, September 24, 2002 8:49 AM

Page 19: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 215

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

specified topic. An icon that looks like a sheet of paper represents each document.

5. Using the Index tab will organize all the documents from the Contents tab al-phabetically. This tab looks like a piece of paper with index markers on the righthand side. Entering a topic in the Find text box will search through the index for

Fig. 5.23 Using the Contents tab to search for a document about creating a new file.

DiveIntoSunONE.fm Page 215 Tuesday, September 24, 2002 8:49 AM

Page 20: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

216 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

the first occurrence of the word. Hitting Enter repeatedly acts as a 'find next' op-tion.

6. The third way to use help is through the use of the Search tab, which has an iconof a magnifying glass on the tab. Again a desired topic is entered into the Find

Fig. 5.24 Using the Index tab to search about breakpoints

DiveIntoSunONE.fm Page 216 Tuesday, September 24, 2002 8:49 AM

Page 21: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 217

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

text box. A list of related topics is then brought up for viewing. Clicking a topicwill open up the same document as opened by either of the other help tabs.

7. Using the Search tab will also highlight any occurrences of the word in the text.The number to the left of the document name indicates how many times thesearched word appears in the text. The red dot to the left of the document nameindicates the rating. This rating is how relevant Sun ONE thinks the document isin regards to what the user is looking for. The more filled the circle is the better

Fig. 5.25 Using the Search tab while searching for applet.

DiveIntoSunONE.fm Page 217 Tuesday, September 24, 2002 8:49 AM

Page 22: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

218 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

the chances that the document is what the user was looking for help with. The top-ics are also organized with the most relevant document at the top of the list.

5.7 Compiling and Executing Applications and Applets

5.7.1 A Java Application

The following section is designed to walk through the process of compiling and executingany of the programs that can be found within the Java How to Program, Fourth Editionbook. First the user should copy a version of Fig. 2.1 and place it on their local hard drive.The file can be obtained by using the CD that is located in the back of the Java book. Allexamples that are within the Java How to Program, Fourth Edition can be found on thisCD. The user can access these examples by clicking the Examples button, then openingthe appropriate chapter, and then opening the correct figure number. In this case the file islocated in the ch02 folder and within that, in the Fig02_01 folder.

Fig. 5.26 Sun ONE highlights the searched words and ranks the returned results.

DiveIntoSunONE.fm Page 218 Tuesday, September 24, 2002 8:49 AM

Page 23: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 219

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

1. Click the File menu button and then the Open File... command. Files can also beopened using the Open File icon, which looks like an opening folder. Locate thefile Welcome1.java in whichever directory it was copied into.

2. Once the file is opened Sun ONE will ask if the class needs to be placed in a pack-age. Click Accept to have the class entered into the default package. Notice thatthe folder in which the class resides is now 'mounted' in the Explorer window onthe Filesystems tab. If the knob to the left of the mounted directory is clicked,it will drop down a list of all classes (.java files ) found within that directory.Sun ONE will also open the Source Editor window that contains the code forWelcome1.java.

Fig. 5.27 The Open File command in the File menu.

Fig. 5.28 The Source Editor window for Welcome1.java.

DiveIntoSunONE.fm Page 219 Tuesday, September 24, 2002 8:49 AM

Page 24: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

220 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

3. Right-clicking on the Filesystems object in the Explorer window can alsoopen the file. Select Mount > Local Directory and then choose the directorywhere the Welcome1.java file is located. This will mount the directory just asSun ONE did when the file was opened. There is also a Mount Filesystem…command under the File menu.

4. To compile the program select the Compile command from the Build menu.This task will compile the program and report any errors that it might contain.

Fig. 5.29 Mounting a directory by right-clicking on the Filesystems object.

Fig. 5.30 Compile command found within the Build menu.

DiveIntoSunONE.fm Page 220 Tuesday, September 24, 2002 8:49 AM

Page 25: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 221

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5. Once the program has been compiled an Output Window will pop up and tellthe user that it Finished Welcome1. This is the result of compiling a programthat has no syntax errors.

6. To actually run the program it needs to be executed. This can be done by going tothe Build menu and clicking the Execute command. Clicking the Execute iconcan also be used to run the program. The Execute icon looks like a green playbutton and is located in the tool bar of the Sun ONE window. When a program isexecuted the compiler will automatically compile the program beforehand.

Fig. 5.31 Output Window for compiling Welcome1.java.

Fig. 5.32 Executing the program from the Build menu.

DiveIntoSunONE.fm Page 221 Tuesday, September 24, 2002 8:49 AM

Page 26: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

222 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

7. Executing the program will cause Sun ONE to switch to the Running tab locatedat the bottom of the main Sun ONE window. Another Output window will popup and display the output for the program in the Output Window screen.

8. To get back to the Source Editor window click the Editing tab.

5.7.2 A Java Applet

Compiling and executing an applet in Sun ONE is similar, though not exactly the same asexecuting an application. Applets are designed to run in a Web browser which is not thetraditional way a program is run. Sun ONE provides an Applet Viewer program that allowsan applet to be run in the Sun ONE IDE. Again, first copy the files from the fig03_06 fold-er in the ch03 folder within the Exercises button on the Java CD.

1. Then go to the File menu and click Open File. Locate the WelcomeAp-plet.java file and open it. Again, click Accept to keep the default package

Fig. 5.33 Output Window for executing Welcome1.java.

Fig. 5.34 Running and Editing tabs found on Sun ONE window.

DiveIntoSunONE.fm Page 222 Tuesday, September 24, 2002 8:49 AM

Page 27: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 223

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

setting. This time notice that in the mounted folder there is a WelcomeAp-plet.java file and a WelcomeApplet.html file.

2. To compile the applet go to the Build menu and click Compile. The file can alsobe compiled by right-clicking on the project in the Explorer window. SelectCompile from the list to compile the applet.

Fig. 5.35 Opening WelcomApplet.java using the Open window.

Fig. 5.36 Compiling the WelcomeApplet.java project.

DiveIntoSunONE.fm Page 223 Tuesday, September 24, 2002 8:49 AM

Page 28: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

224 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

3. If a user tries to execute the applet they will be transferred to the Running tabwhere they might receive a NoSuchMethodError: main in the Output Win-dow screen. Applets do not run like traditional programs. They run through anyWeb browser (Internet Explorer, Netscape Navigator) using the provided HTMLfile, or they run through the Applet Viewer.

4. If this exception occurs, right-click the WelcomeApplet project name and se-lect Properties to enable the Applet Viewer. This will bring up a window thathas an Execution tab on the bottom. Click this tab and change the Executorproperty to Applet Execution.

Fig. 5.37 NoSuchMethodError: main output for compiling WelcomeApplet.java.

Fig. 5.38 Executor property of WelcomeApplet.java.

DiveIntoSunONE.fm Page 224 Tuesday, September 24, 2002 8:49 AM

Page 29: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 225

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5. Now when the program is executed it will run the applet viewer and not the defaultcompiler. Note that if the New Wizard is used to create the applet, this propertyis initially set by Sun ONE.

6. The applet can be closed, like any normal application, by clicking the X in the topright corner or by going to the Applet menu and choosing either Close or Quit.Then click the Editing tab to return to the Source Editor.

Fig. 5.39 WelcomeApplet.java executing property with the Applet Viewer.

Fig. 5.40 Quitting the applet through the Applet menu.

DiveIntoSunONE.fm Page 225 Tuesday, September 24, 2002 8:49 AM

Page 30: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

226 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

7. The applet can also be run by loading the HTML document with either InternetExplorer or Netscape Navigator. To close the applet simply close the Web brows-er.

5.8 Creating and Compiling Applications and Applets

5.8.1 Creating a New Application

In order to create an application that is not found on the Java CD the New Wizard mustbe used. This wizard is used to generate an empty class to be programmed any way the userdesires. The wizard also contains many templates that are used to generate portions of codeto aid the user. These templates range from the creation of simple classes and Java Beansto the creation of sockets or even new Swing components. We encourage the use of theEmpty template in this section because it allows the user to manually type some code andbecome familiar with the language. The following section provides an overview of the ad-ditional templates in the New Wizard.

Fig. 5.41 Running WelcomeApplet.java through Internet Explorer 6.0.

Fig. 5.42 Running WelcomeApplet.java through Netscape Navigator 6.2.

DiveIntoSunONE.fm Page 226 Tuesday, September 24, 2002 8:49 AM

Page 31: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 227

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

1. To create a new application, click the File menu and select New…. Clicking theNew… icon will also accomplish this task. The New… icon looks like a piece ofpaper with a star on the bottom right-hand corner.

2. Sun ONE will then open up the New Wizard. In the Choose Template screenselect Classes from the list of templates. Double-clicking will drop down a listof templates to select from. Select Empty to generate a “source editor” that isblank.

Fig. 5.43 Selecting New from the File menu.

Fig. 5.44 The Template Chooser screen of the wizard.

DiveIntoSunONE.fm Page 227 Tuesday, September 24, 2002 8:49 AM

Page 32: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

228 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

3. Clicking Next will bring up the Target Location screen where the class can benamed, packaged and mounted. Give this class the name Add, with no packageand mount it in the desired directory.

4. The Finish button will end the wizard and construct a class based on the informa-tion entered. It will create a new class in the directory specified and then open theSource Editor window so the class can be modified. Clicking Finish at any

Fig. 5.45 The Target Location screen of the New Form Creation Wizard.

DiveIntoSunONE.fm Page 228 Tuesday, September 24, 2002 8:49 AM

Page 33: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 229

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

point will end the wizard and leave the rest of the creation screens at their defaultvalues.

5. Enter the code:

Fig. 5.46 Empty file generated by Sun ONE.

1 // Add.java example2 // Adds two numbers together34 public class Add {56 public static void main ( String args[] )7 {8 int x = 7, y = 5;9 System.out.println( "The sum is " + ( x + y ) );

10 11 } // end method main12 13 } // end class add

Fig. 5.47 Code to be input into Source Editor window for Add.java.

DiveIntoSunONE.fm Page 229 Tuesday, September 24, 2002 8:49 AM

Page 34: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

230 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

6. Compile and execute the program.

5.8.2 Creating an New AppletCreating an applet can also be done using the New Wizard. There are still a few dif-

ferences that exist between the creation of an application and an applet. Just as before, theApplet Viewer needs to be used rather than the traditional internal execution of an applica-tion. Sun ONE automatically generates an HTML file to view the applet during execution,or the New Wizard can be used again to create an HTML file.

1. Generating an applet is similar to that of an application. First click New… to openup the New Wizard.

2. On the Choose Template screen select Classes and choose Empty again.

Fig. 5.48 Output for Add.java.

Fig. 5.49 Choosing the Empty class in the Choose Template screen.

DiveIntoSunONE.fm Page 230 Tuesday, September 24, 2002 8:49 AM

Page 35: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 231

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

3. Click Next to bring up the Target Location window. Set the class name toHelloWorldApplet and choose a location for the program to be constructed.

4. Clicking Finish will cause the wizard to end and generate a new class for editing.Add to the code:

Fig. 5.50 Picking the location to place the new class.

1 // HelloWorldApplet.java example2 // Creates an applet that outputs hello world.34 import java.awt.Container;5 import javax.swing.*;67 public class HelloWorldApplet extends JApplet {89 public void init()

10 {11 Container container = getContentPane();12 JTextArea outputArea = new JTextArea();13 container.add( outputArea );14 outputArea.setText( "Hello World!!!" );15 16 } // end method init17 18 } // end class HelloWorldApplet

Fig. 5.51 Code to be input into Source Editor window for HelloWorldApplet.java.

DiveIntoSunONE.fm Page 231 Tuesday, September 24, 2002 8:49 AM

Page 36: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

232 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5. Before the program is compiled, set the executor property to Applet Execution.Right click on the file name in the Explorer window. Select Properties fromthe list. This will bring up the Properties window. Select the Execution tab atthe bottom of the window. Change the value of the Executor property to AppletExecution.

6. Now the program will execute properly. Go to the Build menu and select Exe-cute, or click the Execute icon.

Fig. 5.52 Changing the Executor property to Applet Execution to cause Sun ONE to execute the program using the Applet Viewer.

Fig. 5.53 The output for HelloWorldApplet.java.

DiveIntoSunONE.fm Page 232 Tuesday, September 24, 2002 8:49 AM

Page 37: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 233

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

7. Sun ONE will generate an HTML file to run the applet in either Internet Exploreror Netscape Navigator. To create an HTML file click New again. This time selectthe Other category and choose HTML File from the list.

8. Click Next to advance to the Target Location screen again. Give the file thesame name, HelloWorldApplet, and put it in the same directory. If the Fin-

Fig. 5.54 Creating an HTML file in Sun ONE.

DiveIntoSunONE.fm Page 233 Tuesday, September 24, 2002 8:49 AM

Page 38: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

234 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

ish button is grayed out, type the name of the new target directory in the Pack-age text box.

9. Click Finish to bring up the HTML Source Editor window, which is identicalto that of a java Source Editor window.

Fig. 5.55 Placing the HelloWorldApplet.html file in the same location as HelloWorldApplet.java.

Fig. 5.56 The code generated by Sun ONE for the HTML file.

DiveIntoSunONE.fm Page 234 Tuesday, September 24, 2002 8:49 AM

Page 39: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 235

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

10. Enter the following code in the body of the HTML file:

11. Open either Internet Explorer or Netscape Navigator and load the HTML docu-ment to run the applet.

1 <applet code = "HelloWorldApplet.class" width = 300 height = 300>2 </applet>

Fig. 5.57 Code to be entered into the HTML Source Editor window.

Fig. 5.58 Output for HelloWorldApplet.java using Internet Explorer 6.0.

DiveIntoSunONE.fm Page 235 Tuesday, September 24, 2002 8:49 AM

Page 40: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

236 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5.9 Using the New Form Template Wizard

5.9.1 Creating an ApplicationThe New Wizard is used to generate code for the user. In this example the wizard gener-ates the main method for the class and two data members used in the program. The codeand documentation generated conforms to the Sun ONE standard.

1. To create a new application, click the File menu and select New…. Clicking theNew… icon will also accomplish this task. The New… icon looks like a piece ofpaper with a star on the bottom right-hand corner. Sun ONE will then open up theNew Wizard. In the Choose Template screen, select Classes from the list oftemplates. Double-clicking will drop down a list of templates to select from. Se-

Fig. 5.59 HelloWorldApplet.java running in Netscape Navigator 6.2.

DiveIntoSunONE.fm Page 236 Tuesday, September 24, 2002 8:49 AM

Page 41: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 237

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

lecting Main will generate a class that has a main method already created withinit.

2. Clicking Next will bring up the Target Location screen where the class can benamed, packaged and mounted. Give this class the name Add, with no packageand mount it in the desired directory.

Fig. 5.60 Selecting the Main template in the Choose Template window.

Fig. 5.61 Choosing a location for the file to go.

DiveIntoSunONE.fm Page 237 Tuesday, September 24, 2002 8:49 AM

Page 42: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

238 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

3. Click Next again to show the Basic Class Definition screen. Here you can setsome of the basic properties of a class, such as the class it inherits from or whichinterface it implements. For this example, leave these in default form.

4. To advance to the next screen click Next. This will bring up the Create Fieldsscreen. This screen allows data members to be created. Click the New button tocreate a new data member. Set the Name to x, set the Type to int, and set theInitial Value to 7. Also check the static check box to make the variable static.

Fig. 5.62 The Basic Class Definition screen.

DiveIntoSunONE.fm Page 238 Tuesday, September 24, 2002 8:49 AM

Page 43: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 239

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

Click New to add another variable of Type int with the Name y. In this caseleave the Initial Value blank, but also make the variable static.

5. Clicking Next will advance the wizard to the Override Inherited Methodsscreen. From here the user can select which methods the new class will override.

Fig. 5.63 Making variables in the Create Fields screen.

DiveIntoSunONE.fm Page 239 Tuesday, September 24, 2002 8:49 AM

Page 44: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

240 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

Since this class does not inherit from any other classes there will be no methodsto override.

Fig. 5.64 Override Inherited Methods screen.

DiveIntoSunONE.fm Page 240 Tuesday, September 24, 2002 8:49 AM

Page 45: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 241

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

6. Clicking the Next button yet again brings up the final screen. This is the CreateMethods screen, which allows the programmer to specify additional methods tobe created in the class. No methods are added for this example.

7. The Finish button will end the wizard and construct a class based on the informa-tion provided. It will create a new class in the directory specified and then openthe Source Editor window so the class can be modified. Clicking Finish at any

Fig. 5.65 The final screen; Create Methods.

DiveIntoSunONE.fm Page 241 Tuesday, September 24, 2002 8:49 AM

Page 46: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

242 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

point before this will end the wizard and leave the rest of the options screens setto their default value.

8. Add the line y = 5; to the main method at line 27. Follow this by the line Sys-tem.out.println( "The sum is " + ( x + y ) ); to display theresults. Then compile and execute the program.

Fig. 5.66 The code generated by Sun ONE.

Fig. 5.67 Program output for Add.java.

DiveIntoSunONE.fm Page 242 Tuesday, September 24, 2002 8:49 AM

Page 47: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 243

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5.9.2 Creating an AppletAn applet can also be created with the use of the New Wizard. The wizard will take careof the super class. It can also be used to override inherited methods as shown in this exam-ple.

1. Generating an applet is similar to that of an application. First click New… to openup the New Wizard. On the Choose Template screen select Classes but thistime choose JApplet.

Fig. 5.68 Using the JApplet template to create a new applet.

DiveIntoSunONE.fm Page 243 Tuesday, September 24, 2002 8:49 AM

Page 48: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

244 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

2. Click Next to bring up the Target Location screen. Set the class name to Hel-loWorldApplet and choose a location for the program to be constructed.

3. Click Next again to bring up the Basic Class Definition window. Notice thatSuperclass is already filled in with javax.swing.JApplet. This means

Fig. 5.69 The Target Location screen.

DiveIntoSunONE.fm Page 244 Tuesday, September 24, 2002 8:49 AM

Page 49: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 245

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

that when the source code is generated, the class will extend jav-ax.swing.JApplet.

4. Clicking Next will bring up the Create Fields screen. Clicking Next again willbring up the Override Inherited Methods screen. Notice that since the classinherits from another class, there is a list of methods that we can override. Click

Fig. 5.70 Extending java.swing.JApplet.

DiveIntoSunONE.fm Page 245 Tuesday, September 24, 2002 8:49 AM

Page 50: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

246 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

the check box that says Show Inherited and select the init method. Then clickthe Add button to add the init method to the program.

Fig. 5.71 Overriding the init method.

DiveIntoSunONE.fm Page 246 Tuesday, September 24, 2002 8:49 AM

Page 51: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 247

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5. The Next button displays the final screen. It is the same as before with the excep-tion of the inherited methods, mainly init, being displayed.

Fig. 5.72 The Create Method screen in the New Form Template Wizard.

DiveIntoSunONE.fm Page 247 Tuesday, September 24, 2002 8:49 AM

Page 52: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

248 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

6. Clicking Finish will end the wizard and generate a new class for editing.

7. Add the following lines to the init method:

Fig. 5.73 The code generated by Sun ONE.

1 java.awt.Container container = getContentPane();2 javax.swing.JTextArea outputArea = new javax.swing.JTextArea();3 container.add( outputArea );4 outputArea.setText( "Hello World!!!" );

Fig. 5.74 The code to be added to the init method.

DiveIntoSunONE.fm Page 248 Tuesday, September 24, 2002 8:49 AM

Page 53: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 249

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

8. Compile and execute the program. Sun ONE will automatically run the programusing the Applet Viewer.

9. Sun ONE will also generate an HTML file to run the applet in either Internet Ex-plorer or Netscape Navigator.

Fig. 5.75 HelloWorldApplet.java running through the Applet Viewer.

Fig. 5.76 Internet Explorer 6.0 running the HelloWorldApplet.java applet.

DiveIntoSunONE.fm Page 249 Tuesday, September 24, 2002 8:49 AM

Page 54: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

250 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5.10 Using PackagesThere are several examples in the Java How to Program, Fourth Edition book that are con-tained within custom packages. There are also programs which import those packages touse their functionality. In order to understand packages, we use the Time1.java exam-ple. This requires that the files within the fig08_04 folder be copied from the Java How toProgram CD. Click Exercise and open ch08 to locate the folder.

1. The package imported in this program is com.deitel.jhtp4.ch08. In orderfor Time1.java file to compile correctly, it must be placed in the appropriatedirectory structure.

Fig. 5.77 HelloWorldApplet.java displayed through Netscape Navigator 6.2.

DiveIntoSunONE.fm Page 250 Tuesday, September 24, 2002 8:49 AM

Page 55: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 251

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

2. First, create a folder named com. Inside com, create a folder called deitel. Insidedeitel, create a folder called jhtp4. Finally, create the ch08 folder within jhtp4.Copy Time1.java into the ch08 folder.

3. TimeTest3.java file must be placed on the same level as the com folder. Thisdirectory structure enables TimeTest3.java to correctly import com.dei-tel.jhtp4.ch08.Time1.

4. Mount the folder in which TimeTest3.java and the com folder reside, to en-able the program to compile properly in Sun ONE. To mount the directory, Openthe TimeTest3.java file. Click Accept to keep the default package setting.This causes Sun ONE to automatically mount the directory structure.

Fig. 5.78 The package com.deitel.jhtp4.ch08 in Time1.java.

Fig. 5.79 The importing of com.deitel.jhtp4.ch08.Time1 in TimeTest3.java.

DiveIntoSunONE.fm Page 251 Tuesday, September 24, 2002 8:49 AM

Page 56: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

252 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

TimeTest3.java is located in the root directory and Time1.java is nowavailable in the ch08 folder.

5. Right clicking the Filesystem node and selecting Mount > Local Directory isanother way to mount the directory. Simply choose the directory where com andTimeTest3.java reside.

Fig. 5.80 Opening TimeTest3.java.

Fig. 5.81 Mounting the folder that contains TimeTest3.java and the com folder.

DiveIntoSunONE.fm Page 252 Tuesday, September 24, 2002 8:49 AM

Page 57: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 253

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

6. If TimeTest3.java is compiled, Time1.java will also compile. SinceTimeTest3.java imports Time1.java, both have to be working in order torun or compile the program properly.

5.11 The Sun ONE DebuggerThe Sun ONE IDE provides a debugger tool to help the programmer find run-time logicerrors in programs that compile successfully but do not produce expected results. The de-bugger lets the programmer view the executing program and its data as the program runseither one step at a time or at full speed. The program stops on a selected line of code orupon a fatal run-time error. When the programmer does not understand how incorrect re-sults are produced by a program, running the program one statement at a time and monitor-ing the intermediate results can help the programmer isolate the cause of the error. Theprogrammer can correct the code.

To use the debugger, set one or more breakpoints. A breakpoint is a marker set at aspecified line of code that causes the debugger to suspend execution of the program uponreaching that line of code. Breakpoints help the programmer verify that a program is exe-cuting correctly. A breakpoint is set by clicking the line in the program where the break-point is to be placed and clicking the Add Breakpoint… button in the main window orfrom the Debug menu. When a breakpoint is set, the appropriate line is highlighted in pink.Breakpoints are removed by clicking the Toggle Breakpoint option from the Debugmenu, which adds and removes breakpoints and is also triggered by Shift + F8.

Often certain variables are monitored by the programmer during the debugging pro-cess—a process known as setting a watch. The Watches box in the Debugger Windowallows the user to watch variables as their values change. Changes are displayed in theWatches box after each debugging step. To set a watch, click the Add Watch… buttonon the main window or from the Debug menu and enter the variable name. A watch isdeleted by selecting the watch in the Watch box and pressing the Delete key. Variablevalues can be modified during the debugging process by editing the Value field in theWatch tab.

The main window contains buttons that control the debugging process. These buttonsperform the same actions as the Debug menu items. Each button is labeled in Fig. 5.83[Note: The full debugging menu only becomes available when the debugger is started.] TheStart button starts the application. The Finish button ends the debugging session to let theprogrammer edit and recompile the program before running another test. Pause suspendsprogram execution at the current location.

Fig. 5.82 The output for TimeTest3.java when executed correctly.

DiveIntoSunONE.fm Page 253 Tuesday, September 24, 2002 8:49 AM

Page 58: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

254 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

The Step Into button executes program statements, one per click, including code inmethods that are called, allowing the programmer to confirm the proper execution of themethod, line-by-line. The Step Over button executes the next executable line of code andadvances to the following executable line in the program. If the line of code contains amethod call, the method is executed in its entirety as one step. This allows the user to exe-cute the program one line at a time and examine the execution of the program withoutseeing the details of every method that is called. The Step Out button allows the user tostep out of the current method and return control back to the line that called the method. Ifyou Step In to a method that you do not need to examine, click Step Out to return to thecaller. Click the mouse on a line of code after a number of lines of code you do not wish tostep through, then click the Run to Cursor button to execute all code up to the line wherethe cursor is positioned. This technique is useful for executing loops or methods withouthaving to enter the loop or method.

Breakpoints are persistent, meaning when a project is closed and reopened, any break-points set during a previous debugging session remain set. You can gather informationabout breakpoints by viewing the Breakpoints box in the Debugger Window(Fig. 5.84).

Fig. 5.83 Debug toolbar.

Fig. 5.84 Debugger Window.

Start

Continue

Attach…

Finish

Pause

Step Into

Run to Cursor

Step Over

Step Out

Toggle Breakpoint

Add Breakpoint

Add Watch…

HTTP Monitor

DiveIntoSunONE.fm Page 254 Tuesday, September 24, 2002 8:49 AM

Page 59: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 255

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

The Breakpoints box displays all the breakpoints currently set for the program. Eachbreakpoint can be enabled or disabled by changing the Enabled property to True orFalse. A disabled breakpoint will still exist in the list of breakpoints, but will not cause thedebugger to stop and can be re-enabled at a later time. Sun ONE also allows breakpoints tobe enabled when certain conditions are true. The programmer specifies the condition in theCondition field of the Breakpoints box.

The remainder of this section will guide you through the debugging process for asimple Java application and a Java applet.

5.11.1 Debugging a Java Application

This section guides the user through the debugging process for a simple Java application,DebugApplication.java. This application obtains a number from the user and dis-plays the integers from 1 to the number entered.

1 // DebugApplication.java2 // Sample program for demonstrating the debugger34 // Java extension package5 import javax.swing.JOptionPane;67 public class DebugApplication {8 9 public static int getNumber() {

10 // prompt for a number11 String input = JOptionPane.showInputDialog(12 "Enter an integer: " );13 14 return Integer.parseInt( input ); // convert to int15 16 }1718 public static void main ( String args[] ) 19 {20 int number = getNumber();21 22 // determine whether integer is positive or negative23 if ( number <= 0 )24 System.out.println( "Non-Positive Number Entered" );25 else26 for ( int i = 1; i <= number; i++ )27 System.out.println( i );28 29 System.exit( 0 );30 31 } // end method main3233 } // end class DebugApplication

Fig. 5.85 Code for DebugApplication.java.

DiveIntoSunONE.fm Page 255 Tuesday, September 24, 2002 8:49 AM

Page 60: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

256 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

1. With DebugApplication.java open in the Editing workspace, select AddBreakpoint… from the Debug menu to bring up the Add Breakpoint dialog.Notice that multiple types of breakpoints can be set (i.e., for a specific method orclass). For this example, the breakpoint type is Line. In the Settings section,change the Line number field to 24. Also, verify that both actions (Suspenddebugging and Print text) are activated. When complete, click OK to add thebreakpoint.

2. Notice that line 24 is now highlighted pink in the source window. This indicatesthat a breakpoint is set on that line. Repeat step 1, only this time set the breakpointon line 26. When complete, the source window should appear as displayed(Fig. 5.86).

3. To set a watch, select Add Watch… from the Debug menu. In the WatchName field of the Add Watch dialog, enter the text 'number.' This will set awatch for the variable number. No visual modifications occur within the sourceeditor, however, the watch is now listed in the Debugger Window. To verifythe watch was added successfully, select Debugger Window from the Debugmenu, then view the Watches box. Information about each variable is displayedwhen it is selected. A watch should also be added for the variable i. This variable

Fig. 5.86 Breakpoints indicated by pink highlight.

DiveIntoSunONE.fm Page 256 Tuesday, September 24, 2002 8:49 AM

Page 61: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 257

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

will have continually changing values throughout the execution of the program.[Note: The lack of information about variables number and i is because the de-bugger is not yet running.]

4. To start the debugger, select Start from the Debug menu. At this point, the pro-gram is executing and the Input dialog is displayed. Enter 10 into the text fieldand hit Enter. The program briefly resumes execution then suspends. TheWatches tab in the Debugger Window now displays information about thevariable number. This variable is used to store the number entered by the user,therefore its value is 10.

5. The message displayed in the Output Window states the program has reachedthe breakpoint on line 26. Notice that line 26 is now highlighted green in theSource Editor window, indicating that execution has suspended at this line.When execution is suspended, the programmer can select Run to Cursor, StepOver, Step Into, Step Out or Continue from the Debug menu. After clickingStep Into and Step Over several times check the value of i in the Watchestab. The output window will display the numbers based on how many times theprogrammer clicks Step Into. Its value should have increased by 1 for each pass

Fig. 5.87 The Watches tab in the Debugger window.

DiveIntoSunONE.fm Page 257 Tuesday, September 24, 2002 8:49 AM

Page 62: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

258 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

through the for loop. When finished choose Continue to traverse to the end ofthe program.

6. The main window indicates that the debugger has completed and the text 1through 10 is displayed in the output window. Even though a breakpoint was seton line 24, the program never suspended on that line because the code on line 24never executed. The code on lines 24 and 26, respectively, output text or enter aloop into the output window, depending on the number entered by the user. Obvi-ously, both conclusions cannot be reached, therefore only one of these lines exe-cutes. Start the debugger again, (Step 4) but enter a non-positive number into theInput dialog and observe how the debugger operates.

5.11.2 Debugging a Java Applet

This section expands on the concepts of the previous section by debugging a simple Javaapplet, DebugApplet.java. To the end user, this applet executes identically to De-bugApplication.java from the previous sub-section. The major difference is thestructure of the code.

The reader should be familiar with the structure of the Java applet as opposed to theJava application. Chapter 3 of Java How to Program, Fourth Edition, Introduction to Java

Fig. 5.88 DebugApplication.java suspended at line 26, as marked in blue.

DiveIntoSunONE.fm Page 258 Tuesday, September 24, 2002 8:49 AM

Page 63: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 259

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

Applets, provides a sufficient overview and should be read before proceeding with thisexample.

1 // DebugApplet.java2 // A Sample program to demonstrate using the debugger.34 // Java extension package5 import javax.swing.*;67 // Java core package8 import java.awt.Graphics;9

10 public class DebugApplet extends JApplet {1112 String message = ""; // output message13 14 // initialize applet by obtaining a number from the user15 public void init() 16 {17 // get number from user18 String input = JOptionPane.showInputDialog(19 "Enter an integer: " );20 int number = Integer.parseInt( input ); // convert to int21 22 loop( number ); // call loop function23 24 } // end method init25 26 // display output27 public void paint( Graphics g )28 {29 super.paint( g ); // call inherited version of method paint30 31 g.drawString( message, 25, 25 ); // draw string at (25, 25)32 33 } // end method paint34 35 // loops if the value is greater than zero36 public void loop( int x )37 {38 // test if integer is positive, negative, or zero39 if ( x <= 0 )40 message += "Non-Positive Number Entered";41 else42 for ( int i = 1; i <= x; i++)43 message += i + " ";44 45 } // end method myFunction4647 } // end class DebugApplet

Fig. 5.89 Code for DebugApplet.java.

DiveIntoSunONE.fm Page 259 Tuesday, September 24, 2002 8:49 AM

Page 64: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

260 Sun ONE Integrated Development Environment Chapter 5

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

1. With DebugApplet.java open in the Editing workspace, set breakpoints atlines 20, 22, 31, 40 and 43. Also, set watches for number and message.

2. In order to start the debugger properly it must first be configured to run as an ap-plet. This is done by going to the Property screen of DebugApplet.java.From there click the Execution tab. Change the Debugger property to AppletDebugging. Then the debugger can be started, by selecting Start from the De-bug menu.

3. When prompted with the Input dialog, enter 6 for a value. Execution suspends atline 20. When ready, select Continue from the Debug Menu.

4. Execution resumes and suspends at line 22, which is a method call. In the Watch-es tab, notice that number now has a value of 6. The code on line 20 assigns thevalue entered (6) to the variable number. Select Step Into from the Debugmenu to enter the method. When ready, select Continue.

Fig. 5.90 Changing the Debugger property to Applet Debugging.

Fig. 5.91 number is assigned the value from the user.

DiveIntoSunONE.fm Page 260 Tuesday, September 24, 2002 8:49 AM

Page 65: Sun ONE Integrated Development Environmentcomscigate.com/csg/environ/SunONE/JHTP4_DiveIntoSunONE_Alph… · 5.2 Getting Started with the Java 2 Software Development Kit 1.4 5.2.1

Chapter 5 Sun ONE Integrated Development Environment 261

© Copyright 1992-2003 by Deitel & Associates, Inc. All Rights Reserved.

5. The next breakpoint occurs at line 43, which is part of a loop. This loop repeatsitself counting from 1 to the number entered by the user. Each value is stored inmessage, which is displayed once the loop terminates. When ready, select Con-tinue.

6. Notice that once again, execution suspends on line 43 because of the nature of aloop. In the Watches window, message now has a value of 1. Select Continueagain and notice that message has a value of "1 2" after execution suspends. Thiswill continue until the loop condition is met, which occurs when the counter hasincremented to the value entered by the user. Select Continue until the loop exits.

7. The final breakpoint occurs at line 31. At this point, the variable message hasthe value "1 2 3 4 5 6," which will be output by the applet. Execution suspendedat lines 40 and 43 before 31 because the prior two are part of a method definition.As previously stated, all of the code within a method definition executes when thatmethod is called. Execution then shifts back to the code after the method call.When ready, drag the applet to an area on the screen where it is viewable withoutbeing the active program and select Continue.

8. If the applet is in the background, attempting to view the output causes the execu-tion to suspend on line 31 once again, even though there is no loop. The nature ofan applet specifies that the method that displays applet output is called by the ap-plet itself and not the programmer. This method is called to re-display the outputwhen you select the applet from the background, causing execution to continuous-ly suspend (because of our breakpoint within this method). If you are unable toview the output because of this, disable the breakpoint on line 31 and Continue.[Note: Breakpoints can be set active or inactive at any moment during the debug-ging session under the premise execution has suspended.]

The debugger has now completed its session. Consider debugging this sample programagain and use Step Over rather than Step Into in step 4. Be aware that utilizing StepOver will, in effect, eliminate the last part of step 4 because the debugger will execute thecode within the method definition without prompting the programmer (except for where abreakpoint is set).

DiveIntoSunONE.fm Page 261 Tuesday, September 24, 2002 8:49 AM