epubjs
Version:
Render ePub documents in the browser, across many devices
111 lines (110 loc) • 12.4 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Using Eclipse</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Using Eclipse"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-APP-A-SECT-3"/>Using Eclipse</h1></div></div></div><p>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 <span class="unicode">→</span>
Open Perspective <span class="unicode">→</span> Java to put Eclipse
into the Java editing <span class="emphasis"><em>perspective</em></span>. 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 <span class="emphasis"><em>Learning Java</em></span> examples appear in Eclipse, as
shown in <a class="xref" href="apas03.html#learnjava3-APP-A-FIG-2" title="Figure A-2. Learning Java examples in Eclipse’s Java editing perspective">Figure A-2</a>.</p><p><a id="I_indexterm_id837907" class="indexterm"/>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.</p><p>The bottom area holds tabs related to Java editing. The tab that is
open in <a class="xref" href="apas03.html#learnjava3-APP-A-FIG-2" title="Figure A-2. Learning Java examples in Eclipse’s Java editing perspective">Figure A-2</a>, 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.</p><p><a id="I_indexterm_id837932" class="indexterm"/>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) <span class="unicode">→</span> Preferences <span class="unicode">→</span>
Java <span class="unicode">→</span> 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.)</p><div class="figure"><a id="learnjava3-APP-A-FIG-2"/><div class="figure-contents"><div class="mediaobject"><a id="I__tt1360"/><img src="httpatomoreillycomsourceoreillyimages1707719.png" alt="Learning Java examples in Eclipse’s Java editing perspective"/></div></div><p class="title">Figure A-2. Learning Java examples in Eclipse’s Java editing
perspective</p></div><p>If there are still some red Xs left, double-click the
<span class="emphasis"><em>README-Eclipse.txt</em></span> 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.</p><div class="sect2" title="Getting at the Source"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-APP-A-SECT-3.1"/>Getting at the Source</h2></div></div></div><p><a id="idx11243" class="indexterm"/>Let’s navigate to a source file. Go to the
<span class="emphasis"><em>Calculator.java</em></span> file located in the
<span class="emphasis"><em>ch19/default package</em></span>. Double-click it to open the
source, as shown in <a class="xref" href="apas03.html#learnjava3-APP-A-FIG-3" title="Figure A-3. Opening Calculator.java">Figure A-3</a>.</p><p>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.</p><div class="figure"><a id="learnjava3-APP-A-FIG-3"/><div class="figure-contents"><div class="mediaobject"><a id="I__tt1361"/><img src="httpatomoreillycomsourceoreillyimages1707720.png" alt="Opening Calculator.java"/></div></div><p class="title">Figure A-3. Opening Calculator.java</p></div></div><div class="sect2" title="The Lay of the Land"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-APP-A-SECT-3.2"/>The Lay of the Land</h2></div></div></div><p>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
<a id="I_indexterm_id838065" class="indexterm"/><span class="emphasis"><em>folds</em></span>. 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 <code class="literal">import</code> 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.</p><p><a id="I_indexterm_id838088" class="indexterm"/>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 <code class="literal">javax.swing</code>
import to <code class="literal">javax.boofa</code> 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
<span class="emphasis"><em>problems area</em></span> in the bottom tab is also linked;
clicking on a problem report opens the corresponding file at the correct
line.</p><p>On the right side of the IDE is the <span class="emphasis"><em>outline
pane</em></span> 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.</p></div><div class="sect2" title="Running the Examples"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-APP-A-SECT-3.3"/>Running the Examples</h2></div></div></div><p><a id="idx11245" class="indexterm"/>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 <span class="unicode">→</span> Run As <span class="unicode">→</span> Java
Application. The Calculator runs, as shown in <a class="xref" href="apas03.html#learnjava3-APP-A-FIG-4" title="Figure A-4. Running the calculator">Figure A-4</a>.</p><p>You may use Run As to tell Eclipse that this file actually is a
standalone class with a <code class="literal">main()</code>
method. Normally, a project has a lot of code and only one or a few
<code class="literal">main()</code> 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 <a id="I_indexterm_id838182" class="indexterm"/><code class="literal">System.out</code>, Eclipse
would have opened a Console tab in the bottom panel to capture the
output. To see this in action, run the
<span class="emphasis"><em>ch10/PrintfExamples.java</em></span> 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 id="I_indexterm_id838201" class="indexterm"/></p><div class="figure-float"><div class="figure"><a id="learnjava3-APP-A-FIG-4"/><div class="figure-contents"><div class="mediaobject"><a id="I__tt1362"/><img src="httpatomoreillycomsourceoreillyimages1707721.png" alt="Running the calculator"/></div></div><p class="title">Figure A-4. Running the calculator</p></div></div></div><div class="sect2" title="Building the Ant-Based Examples"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-APP-A-SECT-3.4"/>Building the Ant-Based Examples</h2></div></div></div><p><a id="idx11242" class="indexterm"/>Some of the chapters include components that must be built
with the supplied Ant build file. For example, the JavaBeans JAR in
<a class="xref" href="ch22.html" title="Chapter 22. JavaBeans">Chapter 22</a> and the Web Application WAR file in
<a class="xref" href="ch15.html" title="Chapter 15. Web Applications and Web Services">Chapter 15</a> have Ant builds. You can run Ant
from within Eclipse simply by right-clicking the
<span class="emphasis"><em>build.xml</em></span> file in the folder and selecting Run As
<span class="unicode">→</span> Ant Build. However, in order to see it
you’ll have to open the Project Explorer using Windows → Show View <span class="unicode">→</span>
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
<span class="emphasis"><em>magicbeans.jar</em></span> file from <a class="xref" href="ch22.html" title="Chapter 22. JavaBeans">Chapter 22</a>, navigate to
<span class="emphasis"><em>sr/ch22/magicbeans/build.xml</em></span>, right-click, and
select Run As <span class="unicode">→</span> Ant Build (the first
option). You may have to use the File <span class="unicode">→</span>
Refresh option to see new files generated by the Ant build in the
project tree.</p></div><div class="sect2" title="Loner Examples"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-APP-A-SECT-3.5"/>Loner Examples</h2></div></div></div><p><a id="idx11244" class="indexterm"/>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.</p></div></div></body></html>