Using Eclipse for Java Programming
NOTE: Before using Eclipse for the first time, in your 15-121 class folder
create a folder MyWorkspace. This folder will be used to store all of your
Java projects.
1. Start Eclipse. (Windows: Find it under All Programs in your start Menu.
Macintosh: Find it under Go > Applications in the Finder.)
2. Select a workspace:
(a) Click on Browse and select the folder MyWorkspace from your folder for
15-121. DO NOT USE ANY OTHER WORKSPACE
(b) Click Launch. After some setup, you will see the Eclipse SDK window
with a Package Explorer tab:
the Eclipse icon
NOTE: The first time
you start Eclipse, you
will see a Welcome
screen first. Click the
X to close it for now.
your username should be here instead
3. Each Java program you write will be stored in its own project. From the
Eclipse menu bar at the top of the screen, choose File > New > Java
Project… You will see the following New Java Project window:
4. If you are starting a brand new Java project without any initial program code:
(a) In the New Java Project window, give the project an appropriate name
that’s easy to identify. For example, type in MyFirstProgram as the
Project Name. You will notice that the Location is automatically updated
to be a folder in your workspace with the same name.
(b) Click Finish. You will see the SDK display your project in the left
column, as shown in the picture on the next page. (If popup window asks
about module-info.java click Don’t Create.)
If you are starting a
Java project using a
folder with Java
code that is already
written for you, go
to Step 6 instead.
5. Each program has one or more classes, one of which must contain a main
method. To add a class to a project:
(a) Be sure that the name of the project folder is highlighted (e.g. see picture
above).
(b) Then, from Eclipse’s menu bar at the top of your desktop, select New >
Class. (See next page for a picture.)
(c) Type in the name of the class in the space provided (e.g.
FavoriteFoodDisplayer).
(d) Select the public modifier.
(e) If this class will contain a main method, check off the appropriate box
under “Which method stubs would you like to create?” (In this example,
our class will have a main method.)
(f) Click Finish. You will see this class appear in the Package Explorer in
your default package.
Check this box
only if your class
requires a main
method.
This warning can be
ignored for 15-121.
(g) Enter the following highlighted Java code exactly as it appears in the
main method as shown below:
If you make any syntax errors, you will see a red X appear to the left of
the line with the error. Click on the X to get more info about the error if
you need help.
For example, if you leave the quotation marks out from the code above,
you will see something like this:
(h) Periodically, save your work using File > Save or Command+s.
(i) If your Java program requires more than one class, you can add additional
classes to your project, repeating the instructions of this step.
(j) When you are ready to run your program, continue with step 7.
6. If you are creating a project using Java files that are already stored in a
folder for you (for example, the folder is named MySecondProgram):
(a) IMPORTANT: Make sure the folder with the Java files is stored inside
your MyWorkspace folder before you go on to the following steps.
(b) In the New Java Project window, enter the name of the same folder as the
Project Name. (A message will appear as you are typing that the project will
be based on the existing source.)
(c) Click Finish. (See pictures on next page.)
Syntax error
If you are starting
a Java project
from scratch, go
back to Step 4.
This line is an
automatically-
generated comment.
You may erase this
line if you wish.
7. If your program has no syntax errors, you can run your program as follows:
(a) In the Package Explorer, you must highlight the Java file that has the
main method in it.
(b) Choose Run > Run As… > Java Application.
(c) If your program has no syntax errors, your program will run and the output
will be displayed in the Eclipse SDK Console window in the bottom right
section:
Make sure the
project name and
folder name are
the same.
REMEMBER:
Drag the project
folder into the
MyWorkspace
folder FIRST.
8. To remove a project from your workspace, select the name of a project in the
Package Explorer (e.g. MyFirstProgram), and then choose Edit > Delete.
You have the option of removing just the project name or all of the code from
your workspace. (Think twice before you do this!)
9. To exit out of Eclipse, choose File > Exit.
Written by Tom Cortina
Updated by Margaret Reid-Miller