Eclipse IDE Tutorial 2018
Eclipse IDE Tutorial 2018
Eclipse IDE Tutorial 2018
for Java
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. Bringing Existing Java Files into Eclipse
7. Command-Line Arguments and User Input
8. Running GUI Applications
9. Using Jar Files
10. Creating Jar Files
11. 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 programs.
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.
First make sure the Java Development Kit (JDK) is already installed on your computer.
See www.skylit.com/javamethods/faqs/GettingStartedJava.pdf for directions.
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 may ask you to choose a “workspace.”
A workspace in Eclipse is just a folder on your computer that will hold your programming
projects. It is a good idea to create a folder that is initially empty; do not use any of the
Eclipse installation folders or any folders that hold original files from your textbook.
Go over the overview and/or tutorials or click on Workbench (the arrow icon) or simply close the
“Welcome” tab to start coding.
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
www.skylit.com/javamethods/faqs/LitvinsPreferences.zip.
You might want to 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.
4 GETTING STARTED WITH ECLIPSE FOR JAVA
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 properly with a newer
version.
GETTING STARTED WITH ECLIPSE FOR JAVA 5
Eclipse configuration settings apply only to the current workspace and revert to defaults
when you switch to a new workspace. You need to configure or import preferences into
every workspace that you create. *
It is possible to always use the same workspace, but as the number of projects in it grows, it may become
hard to manage. You might prefer to use a separate workspace for each chapter in the textbook.
The preferences settings includes an entry for “Installed JREs”, that is, the Java Run-Time Environments
installed on your computer.
Eclipse might not work properly if the JRE selected in the preferences does not match the
JRE installed on your computer.
To examine and change the “Installed JREs” setting, choose Preferences on the Window menu, and go
to JavaInstalled JREs:
Remove the JRE listed there if it doesn’t match the JRE on your computer, and add the one you have.
Check the box for the desired JRE. If in doubt, click on Execution Environments (on the left panel,
under “Installed JREs”) to see how your selected JRE matches the version of Java installed on your
computer.
*
https://www.eclipse.org/forums/index.php/t/1088367/ offers a way to install preferences for all workspaces at once
using Oomph installation editor.
6 GETTING STARTED WITH ECLIPSE FOR JAVA
While at this screen, you might want to attach Java docs to the selected JRE. Click on your JRE, click
Edit, click on it again, and click Javadoc Location. If you had installed the docs locally, browse to
the doc/api folder on your computer (for example,
file:/C:/Program Files/Java/jdk-10.0.1/docs/api/); otherwise enter the URL for the Java
documentation page online (for example, https://docs.oracle.com/javase/10/docs/api/).
Now, when you position the cursor over a library class name in the editor and press Shift+F2 for
contest-sensitive help, Eclipse will open a window with the javadoc description of the library class.
You may want to change some of the Eclipse settings frequently. For example, to change the editor font
size, go to Window/Preferences, navigate to GeneralAppearanceColors and Fonts, expand the
“Java” line, choose “Java Editor Text Font”, click Edit, and choose the font size. To set the console
font, go to GeneralAppearanceColors and Fonts “Debug” line.
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 GeneralEditorsText Editors check “Insert spaces for tabs” and “Show line numbers” if you
want them, and uncheck “Highlight current line” and “Enable drag and drop”:
GETTING STARTED WITH ECLIPSE FOR JAVA 7
Under GeneralStartup 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
GeneralStartup and ShutdownWorkspaces.
Under GeneralWorkspace check “Save automatically before build” and “Show workspace path in
window title”.
8 GETTING STARTED WITH ECLIPSE FOR JAVA
Under JavaCode StyleFormatter click New and enter a name of your choice for a new profile:
GETTING STARTED WITH ECLIPSE FOR JAVA 9
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 ArraysArray initializers uncheck “after
opening brace” and “before closing brace” boxes. Under the “Control Statements” tab check all “Insert
new line” boxes.
Define another profile under JavaCode StyleClean 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 JavaEditorContent Assist uncheck all the boxes in the Insertion section and uncheck “Enable
auto activation”:
Under JavaEditorFolding 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 JavaEditorSyntax Coloring, choose the
JavaStatic fields element, and uncheck the “Italic” box. The same for JavaStatic final fields.
10 GETTING STARTED WITH ECLIPSE FOR JAVA
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:\JavaMethods\Ch02, and import our (or your own) preferences into it.
On the File menu choose NewJava 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:
Java 9 introduced modules — another level in the hierarchy of classes and packages. A
module can combine several packages. Modules are not useful in an educational setting;
students can rely on the dummy unnamed module provided by default.
Eclipse Photon has added features for supporting modules, which are not useful to students,
especially for projects created with older versions of Java. When creating a project in
Photon, and after you typed in the name of your project, click Next and uncheck the
Create module-info.java file box, or click Finish and choose Don't Create on the
popup dialog:
If you want to see the contents of the bin folder (where the .class files are placed), from
the Window menu, choose Show ViewNavigator.
12 GETTING STARTED WITH ECLIPSE FOR JAVA
If you expand the Hello folder in Navigator, you will see the src subfolder:
That’s where Java source files go. If you are starting from scratch, select a project (in this case “Hello”),
then 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 check the feature(s) you want automatically
generated for your class (for example, public static void main):
GETTING STARTED WITH ECLIPSE FOR JAVA 13
Click Finish. Type in the code for your class in the editor:
/**
* Displays a "Hello World!" message on the screen
*/
From the Run menu choose Run, or click the “run” button , or press Ctrl+F11. This will build or
rebuild your project, if necessary, and run it.
It is easy and convenient in Eclipse to have several programs in the same project and
choose which one of them to run.
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.
14 GETTING STARTED WITH ECLIPSE FOR JAVA
There are many keyboard shortcut keys in Eclipse. You can see the complete list in
WindowPreferencesGeneralKeys. In particular,
Ctrl+Shift+F formats the file open in the editor according to the active profile, defined in
Preferences, fixing indentation and removing the white space.
Ctrl+/ can be used to comment a highlighted block of code, thus temporarily disabling the
code. Pressing Ctrl+/ again uncomments the block.
A pop-up dialog will ask you whether you want to copy or “link” the files:
GETTING STARTED WITH ECLIPSE FOR JAVA 15
Choose “Copy files” 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.
See the next section for instructions for running Greetings and Greetings2.
If your program reads data files (.txt, .wav, .gif, etc.), place them into the project folder,
such as Hello, at the same level as src and bin.
If your program expects run-time arguments from the command line, you need to define a run-time
configuration. From the Run menu, choose Run Configurations. Enter the configuration name, for
example, “Greetings”. Under the “Main” tab choose the main class for your program (such as
“Greetings”). Under the “(x)=Arguments” tab enter the program’s “command line” arguments in the top
text area. For example:
16 GETTING STARTED WITH ECLIPSE FOR JAVA
Then click Run. You have to set the run-time arguments once, as long as you keep them the same. When
the Greetings class is selected, the “Run” button and the Ctrl+F11 key will run the correct
configuration.
The Greetings2 program 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 a prompt.
Click on any class to select it, then press Ctrl+F11 to run it.
18 GETTING STARTED WITH ECLIPSE FOR JAVA
As an example, let’s set up a project for the BalloonDraw program (Java Methods, Section 4.2), which
uses the balloondraw.jar library. Create a new project and add BalloonDraw.java to its src
folder:
The project shows a list of errors because several required classes from balloondraw.jar are missing.
To add a JAR library to a project, follow these steps:
1. Select the project (that is highlight its name), 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).
GETTING STARTED WITH ECLIPSE FOR JAVA 19
2. Select Java Build Path and click on the “Libraries” tab and select Classpath:
3. Click Add External JARs..., navigate to the folder that holds the JAR file, choose the file, and
click Open.
20 GETTING STARTED WITH ECLIPSE FOR JAVA
Click Apply and Close. Now the errors disappear and you can run the program.
If you add to your project a class with the same name as a class in the JAR, the explicitly
added class will be used, rather than the class in the JAR.
For example, we can add Balloon.java to the project and edit it. Balloon.class in the project will
take precedence over Balloon.class in balloondraw.jar, that is, your edited version will be used
when you run the program.
If you have the source code for the library and/or javadoc documentation, you can “attach” them to the
JAR file to take full advantage of Eclipse’s interactive tips and to have convenient access to the source
code and the docs. Under the “Libraries” tab expand the newly created library item —
GETTING STARTED WITH ECLIPSE FOR JAVA 21
— click on “Source attachment,” and click Edit... You will see a source attachment pop-up:
Choose “External location”, navigate to the folder that holds the source code (unzip it first, if necessary),
and click OK. Repeat the same for “Javadoc location” if you have API documentation for the classes in
the JAR. Now if you hover over a library element in the editor, Eclipse displays a tip from that element’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. This works for both the standard Java library and your own JAR files.
22 GETTING STARTED WITH ECLIPSE FOR JAVA
The same JAR file can be runnable and can also serve as a library.
Eclipse allows you to quickly create a library or an executable JAR file for a project. From the
File/Export... menu, expand the Java line and choose “JAR file” or “Runnable JAR file”:
Specify the destination and, for a runnable file, the name of the run configuration. See Eclipse help for
details: Help Contents Java development user guide Tasks Creating JAR files. Follow the
instructions for “Creating a new JAR file” or “Creating a new runnable JAR file.”
GETTING STARTED WITH ECLIPSE FOR JAVA 23
As far as the debugger is concerned, see Top Ten Reasons Not to Use a Java Debugger in School.