A.3. Using Eclipse

The first thing we need to do is set up the IDE for browsing and editing our Java source code. Choose Window Screenshot Open Perspective Screenshot 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 CVS repository browsing. You can open additional tools and move things

Screenshot A-1. The import projects dialog box
Java ScreenShot

around to your liking, but the predefined perspectives give you a good start. Now the Learning Java examples appear in Eclipse, as shown in Screenshot 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 tutorial. The bottom area holds tabs related to Java editing. The tab that is open in Screenshot 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 Eclipse is in Java 5.0-compatible mode. Choose Window Screenshot Preferences Screenshot Java Screenshot Compiler and set the Compiler

Screenshot A-2. Learning Java examples in Eclipse's Java editing perspective
Java ScreenShot

Compliance Level to 5.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.

A.3.1. Getting at the Source

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 Screenshot 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.

Screenshot A-3. Opening Calculator.java
Java ScreenShot

A.3.2. The Lay of the Land

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.swing import to 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 line. 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.

A.3.3. Running the Examples

To run the Calculator example, select the source file and choose Run Screenshot Run As Screenshot Java app. The Calculator runs, as shown in Screenshot A-4.

Screenshot A-4. Running the calculator
Java ScreenShot

We chose Run As to tell Eclipse that this file actually a standalone class with a main( ) 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 app. 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 app that printed output to System.out, Eclipse 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.

A.3.4. Building the Ant-Based Examples

Some of the chapters include components that must be built with the supplied Ant build file. For example, the JavaBeans JAR in and the Web app WAR file in 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 Screenshot Ant Build. 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 , navigate to ch22/learningjava/build.xml, right-click, and select Run-As Screenshot Ant Build (the first option).[*] You may have to use the File Screenshot Refresh option to see new files generated by the Ant build in the project tree.

[*] If you had previously installed the Java JRE alongside the JDK, you may get an error indicating com.sun.tools.javac.Main not being in the classpath. To resolve this bug in Eclipse, open the Windows Screenshot Preferences Screenshot Ant Screenshot Runtime Screenshot Classpath tab to add the external JAR tools.jar from the JDK to the Ant classpath.

A.3.5. Loner Examples

Some of the examples in the tutorial may be a little easier to deal with outside of Eclipse, at least until you get immersed in the IDE. For example, some of the networking examples may be easier run on the command line. There's no reason to stay in the IDE for everything. Get comfortable in both environments.