Eclipse PDF
Eclipse PDF
Maria Litvin
Phillips Academy, Andover, Massachusetts
Gary Litvin
Skylight Publishing
1. Introduction
2. Downloading and Installing Eclipse
3. Importing and Exporting the Preferences
4. Configuring Eclipse
5. Running “Hello World”
6. Command-Line Arguments and User Input
7. Bringing Existing Java Files into Eclipse
8. Running GUI Applications and Applets
9. Using Jar Files and Running GridWorld Programs
10. Content Assist and the Debugger
1. Introduction
Eclipse is a vast extendable set of tools for software development. Here we are interested in Eclipse’s
Integrated Development Environment (IDE) component for writing Java software. Eclipse is an open
source project of Eclipse Foundation; you can find information about Eclipse Project at
http://www.eclipse.org/eclipse. Eclipse is available free of charge under the Eclipse Public License.
Eclipse was developed by software professionals for software professionals; it may seem overwhelming
to a novice. This document describes the very basics of Eclipse, enough to get started with Java in an
educational setting.
Eclipse runs on multiple platforms including Windows, Linux, and Mac OS. There may be minor
differences between Eclipse versions for different platforms and operating systems, but the core features
work the same way. Here we will use examples and screen shots from Windows.
This document describes the 2013 version of Eclipse, known as Kepler Release for Java Developers.
First make sure Java (the JDK and Documentation) is already installed on your computer.
See http://www.skylit.com/javamethods/faqs/gettingstarted.html for directions.
Go to http://www.eclipse.org and click on “Download Eclipse.” In the displayed list of installations, click
on “Eclipse IDE for Java Developers” (not EE). In the “Download Links” on the right, choose the desired
operating system and architecture (for example, “Windows 64-bit” if you have a 64-bit system running
Windows.
Under Windows, you will download a zip file, for example, eclipse-standard-kepler-R-
win32.zip (for 32-bit systems) or eclipse-java-kepler-R-win32-x86_64.zip (for 64-bit
systems). Double click to open it and copy the eclipse folder from the zip file into your file system. (It
may take a few minutes to extract Eclipse files from the zip file.) We prefer to put the eclipse folder
into C:\Program Files and rename it Eclipse (with a capital “E”) for consistency with the names of
other application folders. But you can install it in a root directory, for example, C:\eclipse. You will
find eclipse.exe in the eclipse folder. This is the Eclipse executable. Create a shortcut to it on the
desktop (by dragging eclipse.exe to the desktop while holding down Ctrl+Shift or Alt).
Double click on the shortcut or on eclipse.exe. Eclipse comes up with a Welcome screen:
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 3
Go over the overview and/or tutorials or click on Workbench (the arrow icon) to start working.
Eclipse configuration settings apply only to the current workspace and revert to defaults when you switch
to a new workspace. Theoretically it is possible to always use the same workspace, but as the number of
projects in it grows, it may become unmanageable and the Eclipse performance may be degraded. We
prefer to use a separate workspace for each chapter in the book. Luckily, Eclipse provides a way to
export the preferences from the current workspace into a file (an .epf file) and import the preferences
from a file into a workspace.
Our preferences are available in the LitvinsPreferences.epf file. You can download
LitvinsPreferences.zip, which contains LitvinsPreferences.epf, from
http://www.skylit.com/javamethods/faqs/LitvinsPreferences.zip.
If you want, just import these preferences into your workspace and leave Section 4, “Configuring
Eclipse,” until later, when you are ready to experiment with your own settings. Follow these steps:
1. Download LitvinsPreferences.zip and extract from it LitvinsPreferences.epf into a
folder of your choice.
2. Choose the Import... command on the File menu.
3. Expand “General”, select “Preferences” and click Next:
4 GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD
You need to configure or import preferences into every workspace that you create.
If you want to configure your own preferences and save them, perhaps for backup or for using them in
another workspace, export them into a file. Follow these steps:
Preferences created with an earlier release of Eclipse might not work with a newer version.
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 5
4. Configuring Eclipse
In this section we give a few suggestions for setting preferences and show where different types of
options are located. The options we recommend below simply reflect... well, our preferences.
Click on it.
Under GeneralÖEditorsÖText Editors check “Insert spaces for tabs” and “Show line numbers” if you
want them, and uncheck “Highlight current line” and “Enable drag and drop”:
Under GeneralÖStartup and Shutdown uncheck “Confirm exit when closing last window” and all of
the “Plug-ins activated on startup.” Also increase the number of recent workspaces under
GeneralÖStartup and ShutdownÖWorkspaces.
Under JavaÖCode StyleÖFormatter click New, and enter a name for a new profile:
Click OK. Under the “Indentation” tab choose the “Spaces only” tabs policy and set both the indentation
size and the tab size to 2. Under the “Braces” tab change all brace positions, except the last one, “Array
initializer,” to “Next line.” Under the “White Space” tab, ArraysÖArray initializers uncheck “after
opening brace” and “before closing brace” boxes. Under the “Control Statements” tab, check all “Insert
new line” boxes.
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 7
Define another profile under JavaÖCode StyleÖClean Up. Under the “Code Organizing” tab check
“Remove trailing whitespace”; under the “Missing Code” tab uncheck “Add missing annotations.” Under
the “Unnecessary Code” tab uncheck “Remove unused imports.”
Under JavaÖEditorÖContent Assist uncheck all the boxes in the Insertion section and uncheck “Enable
auto activation”:
Under JavaÖEditorÖFolding uncheck all the “Initially fold” boxes or disable folding altogether by
unchecking the “Enable folding” box.
If you do not like italics in your code editor, go to JavaÖEditorÖSyntax Coloring, choose the
JavaÖStatic fields element, and uncheck the “Italic” box.
When you are finished setting the preferences, click OK. If Eclipse asks you whether it is OK to reload
the workspace, click Yes.
Never use Eclipse’s default workspace. Create your own, for example, C:\mywork or
C:\Ch02, and import our (or your own) preferences into it.
On the File menu choose NewÖJava Project (or click on the pull-down arrow next to the New button
on the toolbar and choose Java Project). A dialog box pops up. Enter the project name, for example,
“Hello”; leave the “Use default location” box checked:
Click Finish.
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 9
If you find the Package Explorer view too cluttered or want to see the contents of the bin
folder (where the .class files are placed), from the Window menu, choose
Show ViewÖNavigator.
If you expand the Hello folder in Package Explorer, you will see the src subfolder:
That’s where Java source files go. If you are starting from scratch, click on the New Java Class button
on the toolbar. In the dialog box that pops up, enter the name for your class (for example, HelloWorld)
and the features you want automatically generated for your class (for example public static void
main). Click Finish. Enter the code for your class in the editor:
/**
* Displays a "Hello World!" message on the screen
*/
From the Run menu choose Run Last Launched or press Ctrl+F11. This will build or rebuild your
project, if necessary, and run it. When you run your program for the first time, Eclipse might ask you:
Run as Java applet or application? Choose “application.”
It is easy and convenient in Eclipse to have several applets and/or applications in the same
project and choose which one of them to run.
10 GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD
If you press Ctrl+F11 when a Java class is selected or open it in the editor, Eclipse will run the selected
class.
You can double click on any Java file to open it in the editor window.
— and Greetings2:
/*
This program prompts the user to enter his or her
first name and last name and displays a greeting message.
Author: Maria Litvin
*/
import java.util.Scanner;
Greetings expects command-line arguments and Greetings2 accepts input from the user.
If your application expects run-time parameters from the command line, you need to define a run-time
configuration. From the Run menu choose Run Configurations and click on the “(x)=Arguments”
tab. Enter the program arguments in the top text area. For example:
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 11
Then click Run. You have to do it once, as long as you keep the same program arguments.
The third application in our “Hello” project, Greetings2, prompts the user for input.
Unfortunately, Eclipse doesn’t position the cursor correctly on the input line in the console
window. If the cursor is in an editor window and you start typing, you will mess up the
source code. Make sure to click on the console window before responding to the prompt.
A pop-up dialog will ask you whether you want to copy or “link” the files:
Choose “Copy” to copy the Java files into the project folder. If you chose “Link” you will work with the
original files in their original location and you may accidentally ruin or delete them.
If you program reads data files (.txt, .wav, .gif, etc.), place them into the project folder
(such as Hello, one step above src and bin).
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 13
Click on any class to select it, then press Ctrl+F11 to run it.
Eclipse does not need an html file to run an applet in Applet Viewer. If you supply your own html file
in the src folder, Eclipse will copy it into the bin folder when it builds your project. (Eclipse does it to
any file that is not Java source.) You can run your own html file if you select it (click on it) and press
Ctrl+F11. But if you double click on an html file, Eclipse tries to interpret the html tags and,
ironically, can’t find Java. To examine or edit an html file, right click on it and chose
Open WithÖText editor or, for a permanent change, choose Preferences on the Window menu, go to
GeneralÖEditorsÖFile Associations and add “Text editor” to the htm and html file types (and click on
Default to make it the default editor).
(You can test your own html files in a browser, of course, outside Eclipse. Just navigate in your file
manager to the project’s bin folder and double click on the html file.)
14 GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD
Eclipse will show several errors, because GridWorld’s library (gridworld.jar) is not declared in the
project. To add gridworld.jar, select the project then choose Properties on the Project menu (or
right-click on the project name and choose Properties or select the project and press Alt-Enter).
Select Java Build Path and click on the “Libraries” tab:
*
AP and the Advanced Placement Program are registered trademarks of the College Entrance Examination Board;
their use does not constitute endorsement of this document by the College Board.
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 15
Click Add External JARs..., navigate to the GridWorld installation folder GridWorldCode and
choose gridworld.jar. Click OK.
This would be sufficient for running a GridWorld project, but to take full advantage of Eclipse’s
interactive tips and to have convenient access to GridWorld’s source code, it is necessary to “attach” the
source code and javadoc to the jar. In the same dialog, expand the newly created “gridworld.jar” line —
16 GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD
— click on “Source attachment,” and click Edit... You will see a source attachment pop-up:
Click External Folder..., navigate to the external folder that contains GridWorld source
(GridWorldCode) and click OK. Repeat the same for “Javadoc location”: navigate to
GridWorldCode/javadoc and click OK. Click OK when you have added gridworld.jar and attached
the source and javadoc to it.
Now the errors disappear, you can see a “Referenced Libraries” entry in the Package Explorer, and you
have access to the GridWorld’s classes and the source code:
GETTING STARTED WITH ECLIPSE FOR JAVA AND GRIDWORLD 17
If you hover over a GridWorld element in the editor, Eclipse displays a tip from GridWorld’s javadoc:
(Like almost everything else, “hovers” are configurable in Eclipse.) Pressing F2 redisplays the javadoc
tip. Pressing Shift+F2 opens the full javadoc window in the editor.
As far as the debugger is concerned, see Top Ten Reasons Not to Use a Java Debugger in School.