The first thing we need to do is set up the IDE for browsing and editing our Java source code. If you downloaded the standard Java developer version of Eclipse, it should be set up for Java development. If you chose another package, you may need to select Window → Open Perspective → Java to put Eclipse into the Java editing perspective. A perspective in Eclipse is an arrangement of different tools, menu bars, and shortcuts geared toward a particular kind of task, such as Java editing or source repository browsing. You can open additional tools and move things around to your liking, but the predefined perspectives give you a good start. Now the Learning Java examples appear in Eclipse, as shown in Figure A-2.
On the left is the Package Explorer. It shows a tree view of the Java packages, libraries, and resources of our project. Click the folder handles to expand the tree and see source folders for each chapter in the book.
The bottom area holds tabs related to Java editing. The tab that is open in Figure A-2, Problems, shows errors and warnings associated with our project code. Eclipse has already compiled our code in the background. In general, you don’t have to tell it to do so. You’ll also notice red Xs on some of the source folders and files. These files have errors. We’ll talk about why some of our examples are being flagged in a moment. The other tabs, Javadoc and Declaration, give information about the file we’re editing or the source code item selected. The Declaration tab can show a preview of the source for an item selected in the main editor window without requiring you to open it explicitly.
To clear up some of those red Xs, we need to make sure that Eclipse is in Java 7.0-compatible mode. Choose Eclipse (or Window) → Preferences → Java → Compiler and set the Compiler Compliance Level to 7.0. Click OK and select Yes to rebuild the source. (It is also possible to set the compiler level on a per-project basis through the project preferences.)
If there are still some red Xs left, double-click the README-Eclipse.txt file in the project tree to read the latest explanations for these issues. Some of these issues relate to generating source code or installing additional libraries to make the examples work properly.
Let’s navigate to a source file. Go to the Calculator.java file located in the ch19/default package. Double-click it to open the source, as shown in Figure A-3.
When you’re expanding the source folder tree, you can actually continue “deeper” than the Java source file level, expanding elements of the file itself. By moving into the source file in the explorer, you can expose parts of the Java source like methods, variables, and inner classes. By double-clicking these elements, you can open the file and jump right to the corresponding line. This is similar to the outline view that we’ll see in the next section.
In the center of the screen, the open file is displayed in a new
editor tab. The editor is, of course, the center of much of the action
in a Java IDE. Note the color-coded syntax highlighting. Also, notice
the little arrows immediately to the left of the source code. These are
folds. A fold groups a section of
Java code, such as a Java method or comment, and allows you to collapse
or expand it like an outline. For example, the
import statements in the file are folded by
default. Click the blue arrow to the left of the import line to expand
them and see all of the imports.
Next, warnings and errors are highlighted in the column to
the left of the folds column. Try making a small error in the file. For
example, try changing the
javax.boofa and see what
happens. Note all the red Xs indicating problems. When you hover over a
red X, you get a pop-up report of the problem. The column on the right
of the scrollbar provides a view of the location of warnings and errors.
Clicking on these areas takes you to the corresponding line of code. The
problems area in the bottom tab is also linked;
clicking on a problem report opens the corresponding file at the correct
On the right side of the IDE is the outline pane that shows a structured view of the Java code similar to what we saw by expanding the source file tab in the project browser. By clicking on a field, method, or inner class, you jump to the corresponding line in the source editor. Options at the top of the pane let you filter which members are shown for quick access.
To run the Calculator example, you can simply hit the large green arrow Run button while the source file is open or select the source file in the explorer and choose Run → Run As → Java Application. The Calculator runs, as shown in Figure A-4.
You may use Run As to tell Eclipse that this file actually is a
standalone class with a
method. Normally, a project has a lot of code and only one or a few
main() methods. In that case, the Run
menu option (and large green “play” button icon on the toolbar) can be
configured to launch the default class for the overall application. An
individual file can also be launched from its context menu, which you
display by right-clicking on the file (control-click on the Mac). If we
had chosen an application that printed output to
would have opened a Console tab in the bottom panel to capture the
output. To see this in action, run the
ch10/PrintfExamples.java example. To run an example
that requires arguments, you choose the more general Run option from the
Run menu instead. This option pops up a dialog box that lets you
configure, among other things, command-line arguments for the launch.
The Run menu also includes a Run History option with recent launches so
you can run them again quickly.
Some of the chapters include components that must be built with the supplied Ant build file. For example, the JavaBeans JAR in Chapter 22 and the Web Application WAR file in Chapter 15 have Ant builds. You can run Ant from within Eclipse simply by right-clicking the build.xml file in the folder and selecting Run As → Ant Build. However, in order to see it you’ll have to open the Project Explorer using Windows → Show View → Project Explorer. (The Project Explorer shows all files in the project as opposed to the view of the Java class hierarchy shown by the Package Explorer.) There are two Ant Build options. The first runs the default target for the build, while the second lets you choose a target (analogous to Run As and Run in the Run menu). For example, to build the magicbeans.jar file from Chapter 22, navigate to sr/ch22/magicbeans/build.xml, right-click, and select Run As → Ant Build (the first option). You may have to use the File → Refresh option to see new files generated by the Ant build in the project tree.
Some of the examples in the book may be a little easier to deal with outside of Eclipse, at least until you become immersed in the IDE. For example, some of the networking examples may be more easily run on the command line. There’s no reason to stay in the IDE for everything. Get comfortable in both environments.