BUY THIS BOOK
Add to Cart

Print Book $9.95


Add to Cart

Print+PDF $12.93

Add to Cart

PDF $7.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £6.95

What is this?

Looking to Reprint or License this content?


Eclipse IDE Pocket Guide
Eclipse IDE Pocket Guide By Ed Burnette
August 2005
Pages: 127

Cover | Table of Contents


Table of Contents

Chapter 1: Introduction
Welcome to the pocket guide for the Eclipse Integrated Development Environment. This book is the ultimate "no fluff" user's manual for the Eclipse IDE, in particular, its Java Development Toolkit (JDT). This book is designed to get you up and running quickly in the environment even if you've never used Eclipse before. Some Java™ programming knowledge will be helpful when reading this guide, but even if you're new to Java, you can still find a good deal of useful information within these pages. Let's begin with an overview of what Eclipse is and how to download and install it. If you're already using Eclipse, you can skip this section and jump to Part II.
Eclipse is an IDE for "anything, and nothing at all," meaning that it can be used to develop software in any language, not just Java. It started as a proprietary replacement for Visual Age for Java from IBM, but was open sourced in November 2001. Eclipse is now controlled by an independent nonprofit organization called the Eclipse Foundation. Since 2001, it has been downloaded over 50 million times; it is now being used by thousands of developers worldwide. It also has a sizable following in the university community, where it is used in classes on programming and object-oriented design.
Italic
Used for filenames, directory names, URLs, and tools from Unix such as vi. Also used for emphasis and to introduce new terms.
Constant width
Used for names of Java packages, methods, etc.; commands; variables; and code excerpts.
Constant width bold
Used for keywords within code examples and for text that the user should type literally.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
What Is Eclipse?
Eclipse is an IDE for "anything, and nothing at all," meaning that it can be used to develop software in any language, not just Java. It started as a proprietary replacement for Visual Age for Java from IBM, but was open sourced in November 2001. Eclipse is now controlled by an independent nonprofit organization called the Eclipse Foundation. Since 2001, it has been downloaded over 50 million times; it is now being used by thousands of developers worldwide. It also has a sizable following in the university community, where it is used in classes on programming and object-oriented design.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Conventions Used in This Book
Italic
Used for filenames, directory names, URLs, and tools from Unix such as vi. Also used for emphasis and to introduce new terms.
Constant width
Used for names of Java packages, methods, etc.; commands; variables; and code excerpts.
Constant width bold
Used for keywords within code examples and for text that the user should type literally.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
System Requirements
Eclipse runs on today's most popular operating systems, including Windows XP, Linux, and Mac OS X. It requires Java to run, so if you don't already have Java installed on your machine, you must first install a recent version. You can download Java for Windows and Linux from http://java.sun.com; look for the J2SE SDK (Software Development Kit) package without a NetBeans™ bundle. Mac OS X has Java preinstalled. See Table 1 for the minimum and recommended system requirements.
Table 1-1: System requirements for Eclipse
Requirement
Minimum
Recommended
Java version
1.4.0
5.0 or greater
Memory
512 MB
1 GB or more
Free disk space
300 MB
1 GB or more
Processor speed
800 Mhz
1.5 Ghz or faster
In order to unpack Eclipse's download package, you will need a standard archive program. Some versions of Windows have one built in; for other versions, you can use a program such as WinZip (http://www.winzip.com). The other platforms come with an archive program preinstalled.
In the interests of space and simplicity, the rest of this book will focus on the Windows version of Eclipse. Other platforms will be very similar, although you may notice slight platform-specific differences.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Downloading Eclipse
To download the Eclipse IDE, go to http://www.eclipse.org. Click on "downloads" and then select the most recent stable or release version of the Eclipse SDK for your platform. If prompted for a mirror site, pick the one located closest to you. If that one is slow or unavailable, simply return to the download page and try a different mirror, or try the main site.
You may see other download packages such as Runtime, JDT, and RCP on the download page. You don't need those. Just get the one package called Eclipse SDK.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Installing Eclipse
First, install Java if you haven't already. Then download the Eclipse SDK to a temporary directory. Use your archive program to unpack Eclipse into a permanent directory. There are no setup programs and no registry values to deal with.
After you have unpacked the SDK, you should have a subdirectory called eclipse, which in turn has directories in it such as plugins and features. If you don't see these, check the settings on your archive program. A common mistake is to unpack Eclipse in such a way that its directory structure is not preserved. Eclipse won't run unless you unpack it with the exact directory paths that exist in the archive.
You are now ready to launch Eclipse. Inside the eclipse directory, you'll find a launcher program for the IDE called, strangely enough, eclipse (or eclipse.exe). Invoke that program to bring up the IDE.
On Windows, you may find it convenient to create a desktop shortcut to launch Eclipse.
The first time you start Eclipse, you will be prompted for the location of your workspace. The workspace is the location where your source code and other files and settings will be stored on your workstation. Specify a permanent location—somewhere not in your install directory—preferably a location that will be backed up regularly.
Putting the workspace in a different place from where you installed Eclipse makes upgrades easier. See the "Getting Upgrades" section, later in Part I, for more information.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exploring Eclipse
When Eclipse starts up, you will be greeted with the Welcome screen (see Figure 1). This screen provides an introduction for new users who don't have the benefit of a pocket guide to Eclipse; for now you can skip over it by closing the Welcome view (click on the close icon—the x next to the word "Welcome"). You can always come back to the Welcome screen later by selecting Welcome from the Help menu.
Figure 1-1: The Welcome screen allows you to explore introductory material, including examples and tutorials.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Getting Upgrades
Eclipse includes an automatic update facility that can handle point releases (i.e., bug-fix versions) without any work on your part. For example, Eclipse would install an upgrade from 3.1.0 to 3.1.1 automatically. However, for anything more substantial, the best practice is to do a manual clean install.
A clean install is especially important if you want to use beta versions of Eclipse (called Stable or Milestone builds on the download page). Milestone builds are sometimes buggy, so you may need to temporarily go back and run your previous version.
For example, let's say you have been running Version 3.1 for a while and now Version 3.2 has come out. You want to upgrade right away because each new release contains a number of important bug fixes and useful new features. Also, if you have a problem with an older release and report it to the developers, they will simply ask you to upgrade (see "Reporting Bugs" in Part IX). So, you should upgrade, but what's the best way to do it?
First, rename your eclipse directory to something else, like eclipse3.1. Then download the new SDK package and install it normally, as if you had never installed Eclipse before. This is called a clean install because you are not attempting to mix new and old code together. Note that your workspace doesn't need to change at all, but you should back it up before running the new version just in case. Now do you see why I recommended you don't keep your workspace in the install directory?
Any additional plug-ins you have installed for Eclipse will need to be reinstalled at this point unless you keep them in an extension location separate from the Eclipse SDK.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Moving On
Congratulations—you've successfully downloaded, installed, and started exploring Eclipse. In Part II, you'll learn what all the windows and buttons are for and how to set up the environment just the way you like it. If you want to skip ahead and start writing a Java program, jump to Part III.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Workbench 101
Eclipse's main window, called the workbench, is built with a few common user interface elements (see Figure 2). Learn how to use them and you can get the most out of the IDE. The two most important elements are views and editors. If you're already familiar with the Eclipse workbench, you can skim this section or skip to Part III to start programming.
Figure 2-1: The Eclipse workbench is made up of views, editors, and other elements.
A view is a window that lets you examine something, such as a list of files in your project. Eclipse comes with dozens of different views; see Table 2 for a partial list. These views are covered in more detail in Part VII.
Table 2-1: Commonly used Eclipse views
View name
Description
Package Explorer
Shows all your projects, Java packages, and files.
Hierarchy
Displays the class and interface relationships for the selected object.
Outline
Displays the structure of the currently open file.
Problems
Shows compiler errors and warnings in your code.
Console
Displays the output of your program.
Javadoc
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Views
A view is a window that lets you examine something, such as a list of files in your project. Eclipse comes with dozens of different views; see Table 2 for a partial list. These views are covered in more detail in Part VII.
Table 2-1: Commonly used Eclipse views
View name
Description
Package Explorer
Shows all your projects, Java packages, and files.
Hierarchy
Displays the class and interface relationships for the selected object.
Outline
Displays the structure of the currently open file.
Problems
Shows compiler errors and warnings in your code.
Console
Displays the output of your program.
Javadoc
Shows the description (from comments) of the selected object.
Declaration
Shows the source code where the selected object is declared.
To open a view, select Window Show View. The most commonly used views are listed in that menu. To see the full list, select Other….
Most views have a titlebar that includes the icon and name for the view, a close icon, a toolbar, and an area for the content (see Figure 3 for an example showing the Outline view). Note that if the view is too narrow, the toolbar will be pushed to the next line. To discover what all the buttons do, move your mouse over a button, and a little window called a
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Editors
An editor in Eclipse is just like any other editor—it lets you modify and save files. What sets editors in Eclipse apart is their built-in language-specific knowledge. In particular, the Java editor completely understands Java syntax; as you type, the editor can provide assistance such as underlining syntax errors and suggesting valid method and variable names (see Figure 5). Most of your time will be spent in the Java editor, but there are also editors for text, properties, and other types of files.
Editors share many characteristics with views. But unlike views, editors don't have toolbars, and you will usually have more than one of the same type of editor open (for example, several Java editors). Also, you can save or revert an editor's contents, but not a view's. An asterisk in the editor's titlebar indicates that the editor has unsaved data. Select File Save or press Ctrl+S to write your changes to disk.
Figure 2-4: The Java editor provides typing assistance and immediate error detection.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Menus
Eclipse is filled with menus, yet it's not always obvious how to access them. So, let's take a quick tour. The most prominent one is the main menu across the top of the Eclipse window. Click on a menu item to activate it or press Alt and the shortcut key for the menu (for example Alt+F for the File menu).
Some views have view menus that open when you click on the downward-pointing triangle icon near the upper right of the view (see Figure 6 for an example).
Figure 2-5: If you see a triangle in the toolbar, click on it for more options.
Another menu is hidden in the titlebar under the icon to the left of the title. Right-click on the icon to access the system menu; this allows you to close the view or editor, move it around, and so forth. The system menu is shown in Figure 7.
Figure 2-6: Right-click on the icon to the left of the title to get the system menu.
Most commands in Eclipse can be performed in several different ways. For example, to close a view you can either use the system menu or click on the close icon. Use whichever way is most convenient for you.
Finally, you can right-click on any item in the content area to bring up the context menu (see Figure 8). Notice the keyboard shortcuts listed to the right of the menu description. These shortcuts can be used instead of the menu to execute a particular command. For example, instead of right-clicking on main and selecting Open Type Hierarchy, you can just select main and press the F4 key.
Starting in Eclipse 3.1, you can press Ctrl+Shift+L to see a list of the current key definitions. To change them, go to Window Preferences General Keys. By using key definitions and shortcuts, you can work in Eclipse without touching the mouse at all.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Toolbars and Coolbars
A toolbar is a set of buttons (and sometimes other controls) that perform commonly used actions when you click on them. Usually toolbars appear near the top of the window that contains them. A collection of toolbars is called a coolbar (see Figure 9).
Figure 2-8: A coolbar is made up of toolbars. You reorder the individual toolbars by clicking and dragging the separators between them.
Most Eclipse documentation uses the term toolbar to refer to both toolbars and coolbars, so the rest of this book will do the same unless it's necessary to make a special distinction between the two.
In the "Views" section, you saw some examples of toolbars that were part of views. The toolbar at the top of the Workbench window is called the main toolbar (seen back in Figure 2). As you edit different files, the main toolbar will change to show tools that apply to the current editor.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Perspectives
A perspective is a set of views, editors, and toolbars, along with their arrangement on your desktop. Think of a perspective as a way of looking at your work that is optimized for a specific kind of task, such as writing programs.
As you perform a task, you may rearrange windows, open new views, and so on. Your arrangement is saved under the current perspective. The next time you have to perform the same kind of task, simply switch to that perspective, and Eclipse will put everything back the way you left it.
To switch perspectives, select Window Open Perspective or click on the Open Perspective icon (to the right of the main toolbar). This will bring up a list of the most commonly used perspectives; select Other… to see the full list.
Eclipse comes with several perspectives already defined; these are shown in Table 3.
Table 2-2: Built-in Eclipse perspectives
Perspective
Purpose
Resource
Arrange your files and projects.
Java
Develop programs in the Java language.
Debug
Diagnose and debug problems that occur at runtime.
Java Browsing
Explore your code in a Smalltalk-like environment.
Java Type Hierarchy
Explore your code based on class relationships.
Plug-in Development
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Rearranging Views and Editors
Views and editors can be shown side by side or stacked on top of other views and editors. To move a view or editor, simply click on its titlebar and drag it to a new location (see Figure 10). The only restrictions are that editors have to stay in their own rectangular area, and they can't be mixed with views. However, you can arrange the views around the editors, and you can even drag views outside of the main Eclipse window (these are called tear-off views). You can also collapse a view to an icon on the edge of the window (this is called a fast view).
Pay close attention to the changing cursor as you drag a window; the cursor shape indicates where the window will end up when you let go of the mouse button. Table 4 shows the cursor shapes and what they mean.
Figure 2-9: You can see how the Package Explorer is dragged from a tab into the bottom of the window.
Table 2-3: Cursor shapes while dragging views and editors
Cursor shape
Final position of the view/editor being dragged
Above the window under the cursor
Below the window under the cursor
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Maximizing and Minimizing
Sometimes you need to focus temporarily on a single view or editor. For example, you might want to hide all the views and use the whole Eclipse window to look at one large file in the editor. You could resize the editor manually by dragging its edges, but an easier way is to maximize the editor.
Double-click on the view or editor's titlebar (or click on the maximize icon) to make it expand; double-click again (or use the restore icon) to restore the window to its original size. When a window is maximized, you won't be able to see any of the other views or editors outside of the current stack.
As an alternative, you can temporarily shrink the other stacks of windows by clicking on the minimize icon (next to the maximize icon at the top of the view or editor). This hides the content area, showing only the titlebar. It works best on horizontal views and editors.
Remember, you can save your favorite window arrangements as named perspectives.
You could spend hours exploring all the options to customize your Eclipse workbench, but that's not what you're here for, right? Part III will get you started with Java development in Eclipse.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Java Done Quick
Get your stopwatch ready because we're going to create and run some simple Java code as quickly as possible. Ready… set… go!
An Eclipse project is just a directory that holds your program's resources (source code, icons, and so forth). Usually projects are subdirectories in your workspace (see the "Specify a Workspace" section in Part I). You can import an existing project, but for this exercise, we'll make one from scratch.
To create a project, select File New Project… and then double-click Java Project. This opens the New Java Project wizard (see Figure 11).
For "Project name," type in something original like Hello. Under "Project layout," enable the "Create separate source and output folders" option.
As a best practice, always use separate directories for the source and output folders.
Figure 3-1: The New Java Project wizard configures a new directory for your code.
Click Finish to accept the default options and let Eclipse create the project. If you see a dialog that says Confirm Perspective Switch, enable the "Remember my decision" option and click Yes. Also, if you see a dialog about Java 5.0 compliance, enable compliance for the entire workspace (not just the project).
After a moment, you should see your new empty project in the Package Explorer view (see Figure 12).
Figure 3-2: A new Java project is born.
A Java package is a standard way to organize your classes into separate namespaces. Although you can create classes without packages, doing so is considered bad programming practice. To create a new package, select File New Package or click on the New Package icon in the main toolbar ( ). Enter the package name as
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating a Project
An Eclipse project is just a directory that holds your program's resources (source code, icons, and so forth). Usually projects are subdirectories in your workspace (see the "Specify a Workspace" section in Part I). You can import an existing project, but for this exercise, we'll make one from scratch.
To create a project, select File New Project… and then double-click Java Project. This opens the New Java Project wizard (see Figure 11).
For "Project name," type in something original like Hello. Under "Project layout," enable the "Create separate source and output folders" option.
As a best practice, always use separate directories for the source and output folders.
Figure 3-1: The New Java Project wizard configures a new directory for your code.
Click Finish to accept the default options and let Eclipse create the project. If you see a dialog that says Confirm Perspective Switch, enable the "Remember my decision" option and click Yes. Also, if you see a dialog about Java 5.0 compliance, enable compliance for the entire workspace (not just the project).
After a moment, you should see your new empty project in the Package Explorer view (see Figure 12).
Figure 3-2: A new Java project is born.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating a Package
A Java package is a standard way to organize your classes into separate namespaces. Although you can create classes without packages, doing so is considered bad programming practice. To create a new package, select File New Package or click on the New Package icon in the main toolbar ( ). Enter the package name as org.eclipseguide and click Finish. You can see the results in the Package Explorer, as shown in Figure 13.
Figure 3-3: The project has grown a package.
If you looked at the project on disk, you would see the Hello directory, a src directory under that, org under that, and eclipseguide under that. A compact form is shown in the Package Explorer as a convenience.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating a Class
With the org.eclipseguide package highlighted, select File New Class or click on the New Java Class icon ( ). Enter the name of the class, starting with a capital letter. For this example, enter Hello.
Under the section of the dialog that asks which method stubs you would like to create, select the option to create public static void main(String[] args).
Leave the rest of the options set to their default values and click Finish. Eclipse will generate the code for the class for you (this generated class is shown in Figure 14), and open the Java editor with your new class in view.
Whenever Eclipse generates code, it inserts TODO comments to indicate the places you need to edit. Every place in the code that has a TODO comment is listed in the Tasks view (see Part VII).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Entering Code
You could run the program now, but it wouldn't be very interesting. So, let's add a few lines to print something out. Start by deleting the generated comment that says:
	//TODO Auto-generated method stub
Figure 3-4: Now the package has a file in it. You can further expand the file to see its classes.
Then replace it with this code:
	           for (int i = 0; i < 10; i++) {
		System.out.println(
			"Hello, world " + i);
	}
When you're done, the Java editor should look similar to Figure 15.
Figure 3-5: This is 10 times better than the usual "Hello, world" program.
The editor looks innocent enough, but through its clever use of colors and annotations, the window is quietly conveying a great deal of information. A large number of options to control this information can be found under Window Preferences Java Editor.
Press Ctrl+Shift+F (or select Source Format) to reformat your code and fix any indentation and spacing problems. Do this early and often. If you'd like, you can customize the formatting rules in the Java preferences.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Running the Program
Press Ctrl+S (or select File Save) to write the code to disk and compile it. In the Package Explorer, right-click on Hello.java and select Run As Java Application. The program will run, and the Console view will open to show the output (see Figure 16).
Figure 3-6: Isn't this exciting?
That's it! You've written, compiled, and run your first program in Eclipse in just a few minutes. Now, try it again and see if you can do it in under a minute. My record is 35 seconds. Go ahead, I'll wait.
After you have run the program once, you can press Ctrl+F11 (Run Run Last Launched) or click on the Run icon in the toolbar( ) to run it again.
Now that you're ready to write the next killer app, what's the rest of the book for? Part IV will introduce you to your new best pal, the Java debugger. If your programs never have any bugs (ahem), you can skip ahead to Part V to learn about unit testing or Part VI to pick up a few tips about using the IDE.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 4: Debugging
Let's face it: all but the most trivial programs have bugs in them. Eclipse provides a powerful debugger to help you find and eliminate those bugs quickly. This part of the book will give you a head start in understanding how to use the Eclipse debugger.
Running your program under the control of the debugger is similar to running it normally. Right-click on the file containing your main method (Hello.java) and select Debug As Java Application. Or, if you have run or debugged the program before, just press F11 (or select Run Debug Last Launched), or click on the Debug button ( ) in the main toolbar.
Go ahead and try that now. What happened? The program ran to completion and sent its output to the Console view just as if you had run the class normally. You have to set a breakpoint to actually take advantage of the debugger.
A breakpoint is a marker you place on a line of code where you want the debugger to pause execution. To set one, double-click in the gutter area to the left of the source line. For this example, we want to stop on the System.out.println() call, so double-click in the gutter next to that line. A breakpoint indicator will appear, as shown in Figure 17.
Figure 4-1: Set a breakpoint by double-clicking to the left of the source line.
Now, press F11 and Eclipse will run your program again in debug mode. The breakpoint indicator will change when the class is loaded, and the debugger will stop at the line where you added the breakpoint.
One of the nice things about breakpoints in Eclipse is that they stay with the line even if the line number changes (e.g., due to code being added or removed above it).
When the breakpoint is reached and the program stops, you'll notice several things. First, Eclipse will switch to the Debug perspective. If you see a dialog asking to confirm the perspective switch, select "Remember my decision" and click Yes.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Running the Debugger
Running your program under the control of the debugger is similar to running it normally. Right-click on the file containing your main method (Hello.java) and select Debug As Java Application. Or, if you have run or debugged the program before, just press F11 (or select Run Debug Last Launched), or click on the Debug button ( ) in the main toolbar.
Go ahead and try that now. What happened? The program ran to completion and sent its output to the Console view just as if you had run the class normally. You have to set a breakpoint to actually take advantage of the debugger.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Setting Breakpoints
A breakpoint is a marker you place on a line of code where you want the debugger to pause execution. To set one, double-click in the gutter area to the left of the source line. For this example, we want to stop on the System.out.println() call, so double-click in the gutter next to that line. A breakpoint indicator will appear, as shown in Figure 17.
Figure 4-1: Set a breakpoint by double-clicking to the left of the source line.
Now, press F11 and Eclipse will run your program again in debug mode. The breakpoint indicator will change when the class is loaded, and the debugger will stop at the line where you added the breakpoint.
One of the nice things about breakpoints in Eclipse is that they stay with the line even if the line number changes (e.g., due to code being added or removed above it).
When the breakpoint is reached and the program stops, you'll notice several things. First, Eclipse will switch to the Debug perspective. If you see a dialog asking to confirm the perspective switch, select "Remember my decision" and click Yes.
Using one perspective for coding and another for debugging is optional, but some people like being able to customize their window arrangement for each task. You can disable this switching in the Run/Debug preferences (Window Preferences Run/Debug).
Next, several new views will open—most importantly, the Debug view (see Figure 18). This view lets you control all the threads of execution of all the programs being debugged. Finally, the line of code where you put the breakpoint will be highlighted to indicate which line will be executed next.
Figure 4-2: The Debug view lets you control and monitor execution of multiple programs and threads.
To continue running after a breakpoint, click on the Resume button in the Debug view's toolbar (
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Single Stepping
Like most debuggers, the one provided by the Eclipse IDE lets you step line by line through your program with one of two commands: step into ( ; F5; or Run Step Into) and step over ( ; F6; or Run Step Over). The difference between the two is apparent when the current line is a method call. If you step into the current line, the debugger will go to the first line of the method. If you step over the current line, the debugger will run the method and stop on the next line.
Try stepping now, by running until your breakpoint is hit and then pressing F6 several times in a row. Watch the highlight bar move around as the current line changes.
If you step into a method call and then change your mind, execute the step return command ( ; F7; or Run Step Return). This lets the program run until the current method returns. The debugger will stop at the line following the line that called the method.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Looking at Variables
The Eclipse IDE provides many different ways to examine and modify your program state. For example, as you single step, you may have noticed that the Variables window shows the current value of all the local variables, parameters, and fields that are currently visible (see Figure 19). You can quickly identify which variables are changing because Eclipse draws them in a different color. If any of the variables are nonprimitives (objects or arrays), you can expand them to look at the individual elements.
To change the value of a variable, first select it in the Variables view. This will make its current value appear in the bottom half of the window, where you can change it. Save the new value by pressing Ctrl+S (or right-click and select Assign Value).
Figure 4-3: The Variables view shows all the values in scope. Changes since the last step or resume are highlighted in red.
When you are coding, try to use the smallest possible scope for your local variables. For example, instead of declaring all your variables at the top of a function, declare them inside the statement blocks (curly braces) where they are actually used. Besides being a good programming practice, this will limit the number of items displayed in the Variables view.
Another way to see the value of a particular variable is to move your cursor over it in the source editor. After a short pause, a tool tip window will appear with the value. See Figure 20 for an example.
Figure 4-4: Hover the mouse over a variable in the Java editor to see its current value.
What if you need to see the value of a Java expression? No problem: just use the mouse or keyboard to select the expression in the editor, then press Ctrl+Shift+D (or right-click and select Display). Eclipse will evaluate the expression (including any side effects) and show the results in a pop-up window (see Figure 21). The expression can be as simple or as complicated as you like, as long as it's valid.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Changing Code on the Fly
Content preview·