You are on page 1of 36

How to use BlueJ with Murachs Beginning Java 2, JDK 5

BlueJ is an Integrated Development Environment (IDE) for Java thats specifically designed for teaching Java at an introductory level. BlueJ is easy to install and use, it is available for free, and it runs on all modern operating systems. This tutorial has been designed to work with our beginning Java book, Murachs Beginning Java 2, JDK 5. Because of that, it doesnt teach Java. To make it easy for you to use this tutorial with our Java book, each topic in this tutorial includes references to the corresponding chapters in the book whenever thats necessary or helpful. How to get started with BlueJ
How to download and install BlueJ How to create a new project How to create a new class How to save and edit source code How to compile source code How to run an application How to open existing projects

2
2 4 4 6 8 10 12

Object-oriented development with BlueJ


How to work with a class diagram How to work with the object bench How to work with inheritance How to work with interfaces How to work with packages How to generate and view documentation

14
14 16 18 20 22 24

Debugging with BlueJ


How to set and remove breakpoints How to step through code How to inspect variables How to inspect the stack trace

26
26 28 28 28

More BlueJ skills


How to work with applets How to view and download the BlueJ documentation

30
30 32

Perspective

34

Mike Murach & Associates, Inc.


murachbooks@murach.com www.murach.com
Copyright 2005 Mike Murach & Assoc. Inc. All rights reserved.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to get started with BlueJ


Before you install BlueJ, you need to install the Java Development Kit (JDK). To do that, you should follow the procedures shown in figures 1-4 through 1-7 in chapter 1 of Murachs Beginning Java 2, JDK 5. In addition, before you begin using this tutorial, you should download the source code for this book from our web site (www.murach.com) and install it on your C drive. For complete instructions on how to do that, please refer to appendix A of our book.

How to download and install BlueJ


Once the JDK is installed and configured for your operating system, youre ready to install BlueJ. Since most Java development is still done under Windows, figure 1 shows how to install BlueJ for Windows. In summary, once you download the exe for the setup file, you run this file and respond to the resulting dialog boxes. If you encounter any problems, you can view the documentation thats available from the BlueJ web site (see figure 16) and consult the troubleshooting tips. If you want to install BlueJ on another operating system such as Mac (OS X) or Linux, you can follow the instructions that are available from the downloads section of the BlueJ website. Or, you can view the documentation thats available from the BlueJ website (see figure 16) to get detailed instructions for these operating systems.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

The BlueJ web site


www.bluej.org

How to download BlueJ


1. Go to the BlueJ web site. 2. Go to the Downloads page. 3. Save the exe file for the setup program to your hard disk. (This file is only 2.67 MB, so it shouldnt take long to download, even with a slow connection.)

How to install BlueJ


4. Run the exe file for the setup program and respond to the resulting dialog boxes. 5. Start BlueJ by selecting the BlueJ icon from the Desktop or from the Start menu. When you start BlueJ for the first time, it will attempt to locate a version of Java to use. If necessary, select the version of Java that you want to use (jdk1.5.0 or later) from this dialog box:

Notes
Although this procedure is for downloading and installing BlueJ for Windows, you can use a similar procedure for non-Windows systems. For information about installing BlueJ on other operating systems or about troubleshooting installation problems, you can refer to the documentation thats available from the BlueJ web site (see figure 16). How to download and install BlueJ

Figure 1

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to create a new project


Figure 2 shows how to create a new BlueJ project. Essentially, a BlueJ project is a directory that contains all of the files that make up an application. To create a new project, select the New Project command from the Project menu. When you do, BlueJ will display a dialog box like the first one shown in this figure. Then, you can navigate to the directory where you want to create the project, and you can enter a name for the project. When you click the Create button on this dialog box, BlueJ creates a directory that corresponds with the project name. In this figure, the project name is Chapter 01 because thats an appropriate name for a sample project for chapter 1 of a beginning Java book. However, as you begin to build more sophisticated projects, you can use more descriptive names for the projects. For example, if youre developing an application that processes invoices, Invoices might be an appropriate name for the project. If you look closely at the directory icons in the New Project dialog box, youll see that the first four icons are different from the rest of the icons. Thats because the first four icons represent directories that contain BlueJ projects, while the rest of the icons dont store BlueJ projects. This makes it easy to tell the difference between directories that contain BlueJ projects and directories that dont. Later in this tutorial, youll learn how to convert a directory that contains Java files to a BlueJ project.

How to create a new class


When you create a new BlueJ project, youll see an empty BlueJ window like the one shown in the second screen in figure 2. This window just contains one icon that identifies the readme.txt file for the project (which you can use to store textual information about the project). To add a class to a project, you can click on the New Class button at the left side of the BlueJ window. When you do, BlueJ will display a dialog box like the one shown in this figure. Then, you can create a class by entering a name for the class and selecting the Class option. In this figure, the class name is TestApp, which is the name of the sample application thats described in chapter 1 of our book. Note that the Create New Class dialog box lets you create more than just classes. For example, it also lets you create interfaces and enumerations. Although interfaces and enumerations technically arent classes, the procedure for creating them is similar to the procedure for creating classes. On the other hand, abstract classes, applets, and unit tests are special types of classes.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

The dialog box for creating a new project

The dialog box for adding a class to a project

Figure 2

How to create new projects and classes

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to save and edit source code


Figure 3 shows how to use BlueJ to save and edit source code. To start, you open a code editor window for the file you want to work with. Then, you can use standard Windows shortcut keystrokes and menus to enter, edit, and save your code. You can use the Class menu to save the file. You can use the Edit menu to cut, copy, and paste text. And you can use the Tools menu to find and replace text. In addition, BlueJ color codes the source files so its easier to recognize the Java syntax. When you create a new class, BlueJ automatically generates some code for you including a class declaration that matches the file name. Usually this is what you want. Since Java is a case-sensitive language, the capitalization used in the Java code must match the capitalization of the file name. If it doesnt, youll get an error message when you try to compile the code. To prevent this from happening, BlueJ automatically creates a new file to store the source code if you use the code editor to change the name of the class. For example, if you changed the name of the class in this figure from TestApp to TestApp2, BlueJ would create a new class named TestApp2.java to store this source code, and it would keep the code for the TestApp class in the TestApp.java class. When you create a new class, BlueJ also generates some code that you wont need for most applications. This code includes some javadoc comments, the declaration for a variable, a constructor, and a method. Most of the time, you can delete the variable, constructor, and method and the related comments. Then, you can modify the comment for the class so it describes the class. If you do, youll end up with source code that looks something like the source code for the class thats shown in this figure. For more information on declaring variables, see chapter 3 of our book. For more information on coding constructors and methods, see chapter 6. And for more information on coding javadoc comments, see chapter 9. In this figure, the only code that has been added to the generated code is the code thats indented within the braces of the class definition. This code defines a main method that prints a message to the console. For now, you can enter this code exactly as its shown (including capitalization), but youll learn more about coding a main method in chapter 2 of the book, and youll learn more about other types of methods in chapters 5 and 6.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

BlueJs code editor with source code in it

How to enter, edit, and save source code


To open a code editor window for a file, double-click on the file in the BlueJ window, or right-click on the file and select the Open Editor command. To enter and edit source code, you can use the same techniques that you use with any text editor. To save the source code, select the Save command (Ctrl+S) from the Class menu. By default, BlueJ includes some boilerplate code that includes javadoc comments, a variable declaration, a constructor, and a method. You can delete this code or modify it to use it in your application. How to save and edit source code

Figure 3

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to compile source code


Figure 4 shows how to use BlueJ to compile the source code for a Java application. The quickest way to do that is to press Ctrl+K to execute the Compile command of the Tools menu. However, you can also click on the Compile button at the left side of the BlueJ window. If the source code compiles cleanly, BlueJ will display a message at the bottom of the code editor window that says, Class compiled no syntax errors. However, if the source code doesnt compile cleanly, BlueJ will display a message at the bottom of the code editor window that describes the error, and it will highlight the lines of code that caused the error. In the second screen in this figure, for example, the statement within the main method is missing a semicolon, which is a common syntax error. Then, you can read the error message, correct the error, and compile the source code again. When you compile a project that contains two or more class files, BlueJ checks each file in the project and compiles any files that need to be compiled. Then, Blue opens a code editor window for each file with compile-time errors and highlights the first error. In addition, BlueJ displays an arrow on the file icon in the BlueJ window to show that it contains a compile-time error. Sometimes, you might want to compile a single class at a time. If so, you can click on the icon for the class in the BlueJ window to select it. Then, you can select the Compile Selected command (Ctrl+Shift+K) from the Tools menu. Alternatively, you can click on the Compile button in a code editor window for the class.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to compile source code

A compile-time error

Figure 4

How to compile source code

10

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to run an application


Once youve compiled the source code for a class that contains a main method, you can run the code in the main method by right-clicking on the class and selecting the main method from the resulting list of methods as shown in figure 5. Then, BlueJ displays a dialog box like the one shown in this figure to indicate that it is about to run the main method. If you click on the OK button, BlueJ will run the main method. In this example, the code in the main method prints text to the console. As a result, BlueJ opens a console window like the one shown in this figure to display the message. In BlueJ, the console window is called the terminal window. When youre done with the console, you can click on the Close button in the upper right corner of the window to close the window and end the application. When youre learning Java, its common to create applications that use the console to get input from the user and to display output to the user. Youll learn how to create this type of application in chapter 2 of our book, and this type of application is used in chapters 2 through 14. On the other hand, a main method can also launch an application that uses a graphical use interface (GUI) to get input from the user and to display output to the user. Youll learn how to code this type of application in chapters 15 through 17 of our book.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

11

How to run an application

The dialog box for the main method

The console

How to run an application


To run an application, right-click on the class that contains the main method for the application and select the main method. Then, select the OK button in the resulting dialog box to run the main method. When the application prints to the console, BlueJ displays a window like the one above (which BlueJ calls a terminal window). To end the application, you can click the Close button in the upper right corner of the console. How to run an application

Figure 5

12

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to open existing projects


When you start BlueJ, it will automatically open the last project you had open. If this isnt the project that you want to work on, you can select the Close command (Ctrl+W) from the Project menu to close it. Then, you can select the Open Project command (Ctrl+O) from the Project menu to open any existing BlueJ project as described in figure 6. For example, you can use this command to open the BlueJ project that was created in figure 2. Its also possible to use BlueJ to open a non-BlueJ project. For example, the directories and files that you can download and install for Murachs Beginning Java 2 are not BlueJ projects. As a result, if you want to use BlueJ to work with these directories and files, you need to convert them to a BlueJ format. To do that, you can use the Open Non BlueJ command from BlueJs Project menu. Then, use the resulting dialog box to open the directory that contains the project. When you do, BlueJ will open the project. When you open a non-BlueJ project, BlueJ adds bluej.pkg and bluej.pkh files to the directory for the project. This converts a non-BlueJ directory to a BlueJ project. Then, when you open a project, youll see that the icons for the BlueJ project directories are different from the standard directory icon (see figure 2). As a result, you can easily tell which directories have been converted to BlueJ projects.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

13

The dialog box for converting a non-BlueJ project to a BlueJ project

The BlueJ window after a project has been opened

Description
To open an existing BlueJ project, select the Open Project (Ctrl+O) command from the Project menu. Then, use the resulting dialog box to select the directory that contains the project. To open an existing non-BlueJ project and convert it to a BlueJ project, select the Open Non BlueJ command from the Project menu. Then, use the dialog box shown above to select the directory that contains the files for the project, and click on the Open in BlueJ button. When you open a non-BlueJ project, BlueJ adds bluej.pkg and bluej.pkh files to the directory to identify it as a BlueJ project directory. Then, the directory will be displayed with a different icon so you can tell which projects have been converted to BlueJ projects.

Note
If you want to use BlueJ with the downloadable applications and exercises for Murachs Beginning Java 2, JDK 5, you can use the Open Non BlueJ command to open the applications and exercise starts and convert them to BlueJ projects. How to open existing projects

Figure 6

14

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

Object-oriented development with BlueJ


BlueJ was specifically designed to make it easier for a beginning Java student to understand the object-oriented programming techniques that are presented in chapters 6 through 9 of Murachs Beginning Java 2, JDK 5. For example, BlueJ automatically maintains diagrams that show the relationships between the classes in a project. These diagrams are based on the Unified Modeling Language (UML) standard thats used for object-oriented analysis and design. In addition, BlueJ provides a way to manually interact and experiment with objects.

How to work with a class diagram


The diagram in figure 7 shows the relationships among the classes in the Line Item application thats presented in chapter 6 of our book. In particular, each dotted arrow in this diagram shows a uses relationship. In other words, the LineItemApp class uses the Validator class, the Product class, the LineItem class, and the ProductDB class. Similarly, the LineItem and ProductDB classes use the Product class. BlueJ automatically creates arrows like the ones shown in this diagram based on the code in the classes. Usually, thats what you want. However, you can also manually add or remove these arrows. To add an arrow, you use the button with the dotted arrow on it at the left side of the BlueJ window. To remove an array, you right-click on it and select the Remove command. When youre working with a class diagram for a project, you may find that you want to add classes to the project. If you want to import all of the classes from a directory, you can select the Import command from the Project menu. Or, if you only want to import a single class, you can select the Add Class From File command from the Edit menu. Either way, BlueJ will copy the specified classes into the directory for the current project, and it will automatically add any uses arrows between the classes. When working with a class diagram, the lines that connect the classes often intersect and overlap and become difficult to read. To make a class diagram easier to read, you can drag and drop the class icons. When you do, the lines between the classes will automatically adjust. Since you dont have much control over how the lines adjust, you may need to fiddle with this for a while until all of the classes are positioned so that the diagram is easy to read.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

15

The BlueJ window for the Line Item application presented in chapter 6

Description
The dotted arrows in this diagram show the uses relationships. For example, the LineItem class uses the Product class. When you create or import a class, BlueJ automatically creates uses arrows like the ones shown in this diagram based on the code in the classes. To make the class diagram easier to read, you can drag and drop the icons for the classes to rearrange them. To remove an arrow, right-click on it and select the Remove command from the resulting menu. To add a uses arrow, click on the button with the dotted arrow, then on the class that uses another class, and then on the class thats being used by the first class. To add a new class to a project, click on the New Class button to create a new class. To import all of the classes in a directory, select the Import command from the Project menu and use the resulting dialog box to select the directory that contains the classes. To import a single class, select the Add Class From File command from the Edit menu and use the resulting dialog box to select the class. To remove a class from a project, right-click on the class and select the Remove command. How to work with a class diagram

Figure 7

16

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to work with the object bench


Figure 8 shows BlueJs object bench, a place where you can test and inspect the objects that your project uses. To create an object, you just right-click on the class and select a constructor of the class. Once you create an object, the object will be placed on the object bench. For example, this figure shows two objects on the object bench: a Product object named product1 and a LineItem object named lineItem1. Once an object is on the bench, you can right-click on it to display a list of available methods. This allows you to see what methods are available from the object and to experiment with them to make sure that theyre working properly. For example, you can use the setQuantity method of a LineItem object to set the quantity for the object. Similarly, you can use the getTotal or getFormattedTotal method to display the total value that is currently stored in a LineItem object. If a constructor or method requires parameters, BlueJ will prompt you to enter the parameters. To enter numeric parameters, simply enter valid numbers. To enter string parameters, enclose the strings in quotes. And to enter an object reference, create an instance of the object on the bench and click on that object or manually enter the name of the object. For example, to create a LineItem object, you can begin by creating a Product object as shown in this figure. Then, you can right-click on the LineItem class and select the constructor that accepts a Product object and an int value. When you do, BlueJ will display a dialog box that prompts you for both of these parameters. To enter the Product object, you can click on the Product object on the object bench, or you can manually enter the name of the Product object (product1, for example). To enter the int value for the quantity, you can enter a valid integer. To view the data thats stored in an object, you can right-click on the object on the object bench and select the Inspect command to display the Object Inspector dialog box for the object. This dialog box shows the values that are stored in the objects instance variables. If the object contains references to other objects, you can double-click on the object reference to view the Object Inspector dialog box for that reference. For example, in the Object Inspector dialog box for the LineItem object, you can double-click on the reference to the Product object to display the Object Inspector dialog box for that object.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

17

The BlueJ window with two objects on the object bench

Object Inspector dialog boxes for the Product and LineItem objects

Description
To create an object and place it on the object bench, right-click on the class, select a constructor of the class, and respond to the resulting dialog box. To execute a method of an object, right-click on the object and select the method. Once you respond to the resulting dialog box, the method will be executed. If a constructor or method requires parameters, BlueJ will prompt you to enter the parameters. To enter a string, enclose the string in quotes. To enter an object reference, click on an object on the object bench or manually enter the name of the object. To remove an object, right-click on the object and select the Remove command. To view the data thats stored in an object, right-click on the object and select the Inspect command. If the object refers to another object, you can display the data for that object by double-clicking on the arrow icon for the object reference. How to work with the object bench

Figure 8

18

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to work with inheritance


The diagram in figure 7 used dotted arrows to show the relationships between the classes in a project that use each other. Now, the diagram in figure 9 uses solid arrows to show inheritance relationships between some of the classes in the Product Maintenance application thats presented in chapter 7 of our book. In this case, the Book and Software classes inherit the Product class. Then, the dotted lines show that the ProductDB class uses all three of these classes, while the ProductApp class uses the Product and ProductDB classes. Once you understand how to work with uses arrows, you shouldnt have any trouble working with inheritance arrows. Most of the time, these arrows are added automatically when you create or import a class that inherits another class. However, if you need to add an inheritance arrow, you can use the button with the solid arrow on it at the left side of the BlueJ window. If you need to remove an inheritance arrow, you can right-click on the arrow and select the Remove command.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

19

The BlueJ window for the Product application presented in chapter 7

Description
The solid arrows in this diagram show inheritance relationships. For example, the Book and Software classes inherit the Product class. Most of the time, BlueJ automatically creates inheritance arrows like the ones shown in this diagram when you create or import a class. To add an inheritance arrow, click on the button with the solid arrow, then click on the subclass, and then click on the superclass. To remove an inheritance arrow, right-click on the arrow and select the Remove command. How to work with inheritance

Figure 9

20

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to work with interfaces


Figure 10 shows the BlueJ diagram for the classes and interfaces in the Product Maintenance application thats presented in chapter 8 of our book. As you can see, BlueJ clearly identifies interfaces by putting <<interface>> at the top of the interface icon. Then, it uses an arrow with a closed head to show that the ProductDAO interface inherits the ProductReader, ProductWriter, and ProductConstants interfaces. In addition, it uses another arrow with a closed head to show that the ProductTextFile class implements the ProductDAO interface. Although an interface is not technically a class, you can click on the New Class button in the BlueJ window to add an interface. Then, you can enter a name for the interface and select the Interface option from the Create New Class dialog box (see figure 2). For more information about coding interfaces, see chapter 8. When you code a class that implements an interface, you may need to refer to the methods specified by the interface. That way, you can be sure that youve implemented all of the necessary methods. To make this easy to do, BlueJs code editor provides a drop-down list that lets you view the documentation for the interface. If the interface doesnt contain any documentation, BlueJ will automatically generate it.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

21

The BlueJ window for the Product Maintenance application presented in chapter 8

The code editor for a class that implements an interface

Description
The BlueJ window clearly identifies interfaces by putting <<interface>> at the top of the interface icon. In this diagram, the ProductDAO interface inherits the ProductConstants, ProductReader, and ProductWriter interfaces. To add an interface to a project, click on the New Class button. Then, enter a name for the interface and select the Interface option from the resulting dialog box (see figure 2). To remove an interface, right-click on the interface and select the Remove command. When coding a class that implements an interface, you can view the documentation for the interface by selecting the Interface option from the drop-down list at the right side of the code editors toolbar. How to work with interfaces

Figure 10

22

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to work with packages


Often, the classes for an application are organized into packages. Compared to using a text editor, BlueJ makes it easy to create packages and to store your classes in packages. To create a new package, you select the New Package command from the Edit menu. This creates a subdirectory within the current directory, and it adds a package icon (the one that looks like a file folder) to the BlueJ window. Then, you can double-click on the package icon to display the contents of that package. And, if you create a new class within a package, BlueJ will automatically add the necessary package statement to that class. Figure 11 shows the packages and some of the classes in the Line Item application thats presented in chapter 9 of our book. To navigate through these packages, you can double-click on a package to navigate down the hierarchy. For example, you can double-click on the murach package to see that it contains the business, presentation, and database packages. Then, you can continue by double-clicking on one of these packages to display its contents. To navigate up the package hierarchy, you can double-click on the <go up> icon. For example, from the murach.business package, you can double-click on the <go up> icon to navigate to the murach package. Then, you can double-click on the <go up> icon again to navigate to the root directory for the project. To remove a package from a project, you can right-click on the package and select the Remove command from the resulting menu. This will delete the directory for the package and all subdirectories and classes within that directory.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

23

The BlueJ window for the Line Item application presented in chapter 9

The murach package

The murach.business package

Description
To add a new package to a project, you can select the New Package command from the Edit menu. This creates a subdirectory within the current directory. If you add a new class to a package, BlueJ automatically adds the necessary package statement to the class. To remove a package from a project, you can right-click on the package and select the Remove command from the resulting menu. This will delete the directory for the package and all subdirectories and classes within that directory. To navigate through existing packages, you can double-click on the package to navigate down the hierarchy, or you can double-click on the <go up> icon to navigate up the hierarchy. How to work with packages

Figure 11

24

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to generate and view documentation


BlueJ also makes it easy to generate and view the documentation for your classes. Figure 12 shows how. First, you make sure that your document has javadoc comments that describe its constructors and methods, and you make sure that your classes are stored in the appropriate packages. (Both of these skills are described in chapter 9 of our book.) Then, you can generate the documentation by selecting the Project Documentation (Ctrl+J) command from the Tools menu. If the project doesnt contain any existing documentation, this command will generate the documentation and display it in a browser. If the project already contains documentation, BlueJ will display a dialog box that asks if you want to regenerate the documentation or if you want to view it in a browser. When you use BlueJ to generate the documentation for a project, it stores this documentation in a subdirectory named doc thats subordinate to the root directory for the project. As a result, you can also view this documentation by navigating a web browser to this directory and viewing the index.html file. However, its usually easier and faster to view this documentation by pressing Ctrl+J and responding to the resulting dialog box.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

25

The documentation for the Line Item application presented in chapter 9

The dialog box for previously generated documentation

Description
To generate or view the documentation for a project, you can select the Project Documentation (Ctrl+J) command from the Tools menu. If the project doesnt contain any documentation, this command will generate the documentation and display it in a browser. If the project already contains documentation, BlueJ will display a dialog box like the one shown above. Then, you can decide whether to regenerate the documentation or just view it in a browser. BlueJ stores the documentation for a project in a subdirectory named doc thats subordinate to the projects root directory. How to generate and view documentation

Figure 12

26

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

Debugging with BlueJ


As you begin to develop complex applications, its inevitable that you will encounter errors in your code. These errors are commonly referred to as bugs. When that happens, you must find and fix those errors. This is commonly known as debugging. Fortunately, BlueJ includes a tool known as a debugger that you should be able to learn to use within 15 minutes or so. Although this tool isnt as powerful as some debuggers provided by professional IDEs, its easy to use, and it provides a great way to get started with debugging.

How to set and remove breakpoints


The first step in debugging an application is to figure out what is causing the bug. To do that, its often helpful to view the values of the variables at different points in the application as its executing. This will help you determine the cause of the bug, which is critical to debugging the application. The easiest way to view the variables at a particular point in an application is to set a breakpoint as shown in figure 13. To do that, you click a line number in the left side of the code editor window. Then, the breakpoint is marked by a red stop sign over the line number. Later, when the application is run, execution will stop just prior to the statement at the breakpoint, and you will be able to view the variables that are in scope at that point in the application. After the application is run, BlueJ will automatically remove all breakpoints. This is usually what you want. However, if you want to remove a breakpoint before you run the application, you can do that by clicking on the red stop sign for the breakpoint. If you experiment with setting breakpoints, youll see that you can only set breakpoints on certain lines of code. For example, you cant set a breakpoint on a method declaration or a blank line. However, you can set a breakpoint on the opening or closing brace of a method. Typically, though, you want to set a breakpoint on a line that contains a statement that you want to debug.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

27

A code editor window with a breakpoint

Description
A breakpoint is indicated by a small stop sign icon thats placed on the line number to the left of the line of code. To set a breakpoint for a line, open the code editor window for the class and click on the line number. This will only work for lines that contain code or braces, not for method declarations or blank lines. To remove a breakpoint, click on it. Once you have set a breakpoint, you can run the application. When the application encounters the breakpoint, the Debugger window will be displayed as shown in the next figure. BlueJ automatically removes all breakpoints when the application finishes running. How to set and remove breakpoints

Figure 13

28

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to step through code


When you run an application and it encounters a breakpoint, execution will stop just prior to the statement at the breakpoint. Once execution is stopped, a yellow arrowhead marks the next statement to be executed. In addition, a Debugger window like the one shown in figure 14 is automatically opened. This window displays all variables that are in scope at that point in the application. You can also use the Step and Step Into buttons on the Debugger window to step through the statements in the application, one statement at a time. This lets you observe exactly how and when the variable values change as the application executes, which can help you determine the cause of a bug. Once you have stepped through the code that youre interested in, you can use the Continue button to continue execution until the next breakpoint is encountered. Or, you can use the Terminate button to end the applications execution. In addition, if the program is stuck in a loop, you can click on the Halt button to pause the applications execution at its current location.

How to inspect variables


When you set breakpoints and step through code, the Debugger window will automatically display the variables that are in scope. In figure 14, the execution point is in the calculateTotal method of the LineItem class. Since all of the instance variables for the LineItem object are in scope, theyre all displayed in the Instance Variables pane. Similarly, the Static Variables pane shows a static variable named count, and the Local Variables pane shows a variable named test thats local to the calculateTotal method. For numeric variables and strings, the value of the variable is shown in the Debugger window. However, you can also view the values for an object by double-clicking on the object reference. For example, double-clicking on the Product variable in this figure would display an Object Inspector dialog box for the Product object like the one shown in figure 8.

How to inspect the stack trace


In the Debugger window, the Call Sequence pane shows the stack trace, which is a list of methods in the reverse order in which they were called. You can click on any of these methods to display the method and highlight the line of code that called the next method. This opens a new code editor window if necessary. In this figure, for example, clicking on the main method in the LineItemApp class would open the LineItemApp class in a code editor window and display the line of code that called the getFormattedTotal method of the LineItem class. This can help you locate the origin of a bug.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

29

The Debugger window

Description
When a breakpoint is reached, program execution is stopped before the line is executed. In the code editor window, the arrow shows the line that will be executed next. In the Debugger window, the Call Sequence pane shows the stack trace, which is a list of methods in the reverse order in which they were called. You can click on any of these methods to display the method. This opens a new code editor window if necessary. The other panes in the Debugger window show the values for all variables that are currently in scope, including static variables, instance variables, and local variables. If a variable refers to an object, you can view the values for that object by double-clicking on it to display an Object Inspector dialog box (see figure 8). To single-step through code, select the Step or Step Into button. These buttons work similarly, unless the next line of code contains a method call. In that case, they work a little differently. To continue executing code until the next breakpoint, select the Continue button. To pause the application execution at its current location, select the Halt button. To end the applications execution, select the Terminate button. How to work with the Debugger window

Figure 14

30

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

More BlueJ skills


So far, this tutorial has presented all of the skills that you need to use BlueJ to develop object-oriented Java applications. However, if you want to develop applets as described in chapter 18, you need to learn how to use BlueJ to work with applets. In addition, if you want more information about BlueJ, you need to know how to view and download its documentation.

How to work with applets


An applet is a special type of class that can be downloaded from an Internet or intranet server and run on a client computer within a web browser. You can add an applet to a project by clicking on the New Class button and then entering a name for the applet and selecting the Applet option from the resulting dialog box. Then, you can enter the code for the applet. For more information about coding applets, see chapter 18 of our book. As you can see in figure 15, BlueJ clearly identifies applets by displaying <<applet>> at the top of the icon for the class. To run an applet, you can rightclick on it and select the Run Applet command from the resulting menu. When you do, a Run Applet dialog box like the one shown in this figure will be displayed. If you want to view the applet in the Applet Viewer, a tool that comes with the JDK, you can select the second option from the Run Applet dialog box. Then, you can enter the height and width of the applet and click on the OK button. When you do, BlueJ generates a web page for the applet, starts the Applet Viewer, and opens the web page for the applet within the Applet Viewer. This allows you to test the applet from outside a web browser. If you want to test the applet from within a web browser, you can select the third option from the Run Applet dialog box. Then, you can enter the height and width of the applet and click on the OK button. When you do, BlueJ generates a web page for the applet, starts the web browser, and opens the web page for the applet within the web browser. When BlueJ generates the web page for an applet, it uses the name of the applet class plus an extension of html. For example, the web page for the applet shown in this figure will be stored in a file named FutureValueApplet.html. If you want to use the HTML thats generated for this web page in another web page, you can open this file, copy the HTML tags for the applet, and then paste them into another web page.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

31

The BlueJ window for the applet presented in chapter 18

The dialog box for running an applet

Description
To add a new applet to a project, click on the New Class button. Then, enter a name for the applet and select the Applet item from the resulting dialog box (see figure 2). To run an applet, right-click on the applet and select the Run Applet command to display the Run Applet dialog box. Then, select the appropriate option to run the applet in the Applet Viewer or a web browser, enter the height and width of the applet in pixels, and select the OK button. When you do, BlueJ generates a web page for the applet and runs the applet. The name of the web page thats generated is the name of the applet class plus an extension of html. For example, the web page for the applet shown in this figure will be stored in a file named FutureValueApplet.html. How to work with applets

Figure 15

32

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

How to view and download the BlueJ documentation


Although this tutorial covers the essential skills for working with BlueJ, theres more that you can do with BlueJ. To learn more about BlueJ, you can use the procedure shown in figure 16 to view and download the documentation for BlueJ. If you want to learn more about working with BlueJs basic features, you can download the BlueJ Tutorial, or you can display it by selecting the BlueJ Tutorial command from BlueJs Help menu. This tutorial covers much of the same information that youve learned here, but it also covers some other features such as the code pad feature. In addition, it includes information about installing BlueJ on Macintosh, Linux, and other systems. If youre looking for more comprehensive information about working with BlueJ, you can view or download the BlueJ Reference Manual. This manual contains more technical information about working with BlueJ. For example, it contains detailed explanations about how to configure BlueJ, and it describes how to use BlueJ to develop and work with unit tests.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

33

How to view and download the BlueJ documentation


1. Go to the BlueJ web site (www.bluej.org). 2. Go to the Documentation page. 3. Click on the link for the BlueJ Tutorial or the BlueJ Reference Manual. This will display a PDF file that contains the documentation that you selected. 4. (Optional) Use the Save command in the File menu to save a copy of the PDF file on your local system so you have it available for future reference.

Description
The BlueJ Tutorial shows you how to work with BlueJs basic features, including how to install BlueJ on Macintosh, Linux, and other systems. The BlueJ Reference Manual contains more comprehensive information about working with BlueJ. You can also display the BlueJ Tutorial by selecting the BlueJ Tutorial command from BlueJs Help menu. How to view and download the BlueJ documentation

Figure 16

34

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

Perspective
In this tutorial, you learned how to use BlueJ to enter, edit, compile, and run a program. You learned how to use BlueJ to work with object-oriented features such as inheritance and interfaces. And you learned how to use BlueJ to debug applications and run applets. These are all of the skills you need to use BlueJ with our book, Murachs Beginning Java 2, JDK 5.

Summary
BlueJ is an Integrated Development Environment (IDE) for Java thats specifically designed for teaching Java at an introductory level. BlueJ is easy to install and use, it is available for free from the BlueJ web site (www.bluej.org), and it runs on all modern operating systems. BlueJ automatically maintains diagrams that show the relationships between the classes in a project. These diagrams are based on the Unified Modeling Language (UML) standard thats used for object-oriented analysis and design, and they include arrows that show uses relationships and arrows that show inheritance relationships. BlueJ includes an object bench that allows you to test and inspect the objects that your project uses. BlueJ includes an object inspector that allows you to view the values that are stored in an object. BlueJ includes a debugger that can help you find and fix any bugs in your applications. To use the debugger, you can set a breakpoint to stop program execution. Then, you can step through the statements in your applications and view the values of variables and the stack trace. The documentation thats available from the BlueJ web site contains additional information about working with BlueJ, including information about installing BlueJ on Macintosh, Linux, and other systems.

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

35

Exercise 1

Use BlueJ to develop an application

Before you do this exercise, you should follow the procedures shown in chapter 1 (figures 1-4 through 1-7) of Murachs Beginning Java 2, JDK 5 to install and configure the JDK. Then, you should follow the procedure in figure 1 of this tutorial to install BlueJ. In addition, we recommend that you download the directories and files for our book from our web site (www.murach.com) and install them on your C drive. For complete instructions, please refer to appendix A of the book. This exercise will guide you through the process of using BlueJ to enter, save, compile, and run a simple application.

Enter and save the source code


1. Start BlueJ. You should be able to do that by clicking on the Start button, pointing to Programs, pointing to BlueJ, and clicking on BlueJ. 2. Select the New Project command from the Project menu. Then, use the resulting dialog box to create a project named Chapter 01 in the c:\java1.5\ch01 directory. 3. Click on the New Class button to display the Create New Class dialog box. Then, enter TestApp for the class name, select the Class option, and select the OK button to create a new class named TestApp. 4. Double-click on the TestApp class in the BlueJ window to open a code editor window for it. Then, modify the generated code so it looks like this (type carefully and use the same capitalization):
/** * This class prints some text to the console to see * if BlueJ and Java are working properly. * * @author << Your name here >> * @version 1.0 */ public class TestApp { public static void main(String[] args) { System.out.println("This Java application has run successfully"); } }

5. Use the Save command (Ctrl+S) in the Class menu to save your changes, and then close the code editor window.

Compile the source code and run the application


6. Click on the Compile button or press Ctrl+K to compile the source code. If you get an error message, read the error message, edit the source code, and compile the application again. Repeat this process until the application compiles cleanly.

36

How to use BlueJ with Murachs Beginning Java 2, JDK 5 www.murach.com

7. Right-click on the TestApp icon and select the main method from the resulting menu. Then, respond to the resulting dialog box to run the application. It should display a console that says This Java application has run successfully. 8. Close the console window.

Introduce and correct a compile-time error


9. In the code editor window, delete the semicolon at the end of the println statement. Then, click on the Compile button to compile the source code. BlueJ should display an error message at the bottom of the code editor window, and it should highlight the line of code that contains the error. 10. Correct the error and compile the file again (this automatically saves your changes). This time, the class should compile cleanly so you can run it again and make sure that it works correctly. 11. Exit BlueJ by selecting the Quit command (Ctrl+Q) from the Project menu.

You might also like