Started with Eclipse 3.4 for Beginning Programmers
CSC 116 - Introduction to
Department of Computer Science
North Carolina State University
to Software Engineering Tutorials
Target Audience: this tutorial is geared toward the
beginning Java programmer who has some experience with Java programming
from the command-line.
1.0 Introducing Eclipse
When you first learned how to write Java programs, you probably
started by writing in a text file, compiling using the
command, then running your program from the command-line. This form of
programming is what we'll refer to as command-line programming.
While command-line programming has many advantages, it can be quite
tedious. After all, your text editor doesn't usually provide much
outside of... well... editing. Professional developers, generally
speaking, tend to gravitate toward what is called an integrated
development environment (IDE) for their programming. The idea of an IDE
is to integrate many different tools into a single program (e.g. code
editing, compiling, running, file management, code navigation,
documenting, etc.) Programming with an IDE can provide some of the
following advantages over command-line programming:
- Compiling is built-in. Whenever you hit "Save", your
code is compiled for you.
- A controlled execution environment means that you can
control how exactly your program will be executed without changing what
your current system is configured for (e.g. running your program using
different Java virtual machines)
- A Graphical User Interface (GUI) provides immediate
feedback on compile errors, warnings, and other problems with your
- IDEs are highly customizable. From formatting and
coloring preferences to programming in various languages and
technologies, IDEs are extremely extensible.
- Code editing and navigation is much easier
because the IDE "understands" the language you're working with (you'll
understand more of what this means with a few examples).
- IDEs support large projects by providing many other
tools not related directly to programming, but with software
development in general.
In this tutorial we'll be covering Eclipse, a professional-grade,
open-source development platform for Java (and many other languages!).
We'll focus on some of the features found in Eclipse that are not found
in command-line programming.
A note about audience. We will be assuming two different
users of this tutorial:
- Home users: users working on their own machine, who can
devote permanent space to their Eclipse work.
- Lab users: users working in public laboratories who
cannot devote permanent space to their Eclipse work. Lab users may need
some special information specific to their lab, such as the proper
location of their workspace. We may defer to your lab instructor
to get this specific information.
2.0 Installing and Starting Eclipse
Lab Users: your lab should already have Eclipse 3.4 (or
similar version) installed. Start Eclipse the way your lab instructor
tells you to. Skip to the Starting Eclipse part.
Home Users: follow these instructions.
- Go to http://eclipse.org and
- Find the
Eclipse for Java Developers link on the
page. Click on the link for your operating system along the right-hand
Figure 1: Downloading the Eclipse for Java Developers
- Select a mirror and download the file. Make sure that you are
downloading a zip file.
- Once the zip file is downloaded, unzip the file to a location
on your computer. I usually choose something like
Remember this location - we will refer to this location as
- Start Eclipse by executing
When you first start Eclipse, you will be asked to select your
workspace. Like this:
Choosing a Workspace
The Eclipse workspace is a permanent location for all of
- Lab users: choose the location of your workspace
according to what your lab instructor tells you. Please note that your
work may not be backed up on laboratory machines, so be sure to copy
your workspace to a safe place before logging out.
- Home users: choose any location of your workspace on
your hard drive. I usually choose something like
The first time you start with a fresh workspace, you will be
Welcome view. Close the
according to the following figure
Figure 2: Closing the Welcome Screen
In most Eclipse versions, your interface looks like Figure 3.
This is called the Java Perspective (which will have this icon
in the upper-right: ). Feel free to mess
around with the windows (called "Views"). If you want to reset the
perspective to how it originally looked, go to
Reset Perspective. If you are not in the Java perspective, use
> Open Perspective > Other... > Java (NOT Java Browsing!)
Figure 3: The Java Perspective
3.0 Our First Eclipse Project
Now that we have our Eclipse workspace set up, let's set up our
first project. An Eclipse project is a grouping of files all
part of one program. For example, you can have many Java files, the
compiled Java .class files, the project's website, and other resources
in a single Eclipse project.
Let's start by creating a project called
- Right click in the
Package Explorer () and select
Java Project as shown in Figure 4.
Figure 4: Creating a new Java Project
- A window will pop up where you can change setting related to
your project. Place
HelloWorld as the name of your
project. The other default settings are okay.
Figure 5: Configuring your new project
Finish. You should see your new project
appear in the
Package Explorer. Click on the plus sign () to expand and look inside your project. Note:
for Mac and Vista users, this may be an arrow () instead of a plus sign.
Figure 6: Examining your new project
- Notice the
src folder in your project. This is
where we'll put all of our Java classes. So let's create a Java class
HelloWorld.java. Right-click on the
folder, and select
New > Class.
Note: in this case, we are creating a project called
HelloWorld and a class
HelloWorld. These do not
need to be the same.
Figure: 7: Creating a new Java class
- In the
Name box type
the name of your class (you do not need the .java extension). Also,
check the box that says
public static void main(String
args) as method stub that you would like generated. Your screen should
look like this.
Figure 8: Naming and Configuring the New Class
Finish. Eclipse generated a file called
placed stubs for comments and the
main method. A Java
editor has also been opened.
Figure 9: Editing our new class
- Place the print statement for the string literal "Hello,
System.out.println("Hello, World"); . Press the
Save () button at the top (or hit Crtl+S).
Figure 10: Adding our print statement and saving.
- When you just hit Save there, that compiled the program -
equivalent of doing
javac HelloWorld.java on the
command-line. If there are any compile errors, they will pop up in the
Problems view () after you
hit Save. If there are no compile errors, then right-click on
Package Explorer and select
> Java Application.
Figure 11: Running a Java program
- The output to your program gets printed out to the Eclipse
view (). We just ran our first Java
program in Eclipse!!
Figure 12: Viewing console output
4.0 Code Editing Tools
Now that we can create and run a program, let's write some more
code to demonstrate some of Eclipse's powerful code editing tools. We
will cover the following features:
- Code Templates
- Quick Fix
- Code Formatter
- First, let's define a variable in our
class. Place this at the top of your
main method and then
hit Save :
String hello = "Hello, World";
- Now let's change our print statement to print out the contents
hello variable. Delete the string literal in your
print call, and type
h (JUST the letter h!). Like this:
Figure 13: Beginning to replace the string literal with a variable
- Now press
Ctrl + Space. Your text cursor should
be just to the right of the
h (as if you were in the
middle of typing the variable
hello). Here's what the menu
Figure 14: Auto-complete menu
- This is the auto-complete menu. Note that our
hello is at the top of the
Enter to add the rest of the variable's name.
- Save and re-run your program to make sure everything works.
You can use the run menu the way we covered in the previous section, or
to run the previously launched program, hit
Ctrl + F11.
Note: for Mac users, try
Apple + F11 for re-running
- Remove the variable
hello from the print call and
re-run this procedure without looking at the instructions - practice
Any time you want to finish something that you think Eclipse can guess,
it's worth hitting
Ctrl + Space
to see if it does. Eclipse
will find things like method names, variables in the current scope,
class names, or even make suggestions for a variable you're declaring!
The idea is that you don't have to remember everything
the program you're writing. You can select the options with your mouse,
but it's probably faster to use the arrow keys and the
Code Templates and Formatter
- In our
HelloWorld program, delete the
Figure 15: Start with an empty class.
- With your text cursor in the program, start typing the word
Figure 16: The Main method code template
- There should be a code template (with the following icon: ) for the
main method. Hit
to select it.
- Voila! There's our
main method! Note that the
text cursor lands in the middle of the method so you can just start
Figure 17: The Main Method
- Eclipse has another helpful code template called
sysout template the same way you did the
main template. (If you type all of
sysout before hitting
+ space, the menu doesn't pop up because there's only one option!).
- Now that we have our
from the code template, type your string literal of
World!" in it. Re-run your program to ensure that it's correct.
Figure 18: The full Hello, World program
- Once you know that your program works, take a look at your
code. If it looks like Figure 18, it's not indented properly. Before
you start formatting yourself, hit
Ctrl + Shift + F (or go
Source > Format in the menu; Mac users try
+ Shift + F).
Figure 19: Formatted HelloWorld
- Formatted! Now see how fast you can write a fully-formatted
HelloWorld program. Delete the
main method and start over
again and practice until you're satisfactorily fast at it. Use the
following techniques we've covered:
main method code template
sysout code template
- The code formatter
- If you want to add/edit code templates or the way your code
gets formatted, go to
Window > Preferences (Mac Users
Eclipse > Preferences.
- For code templates, go to
Java > Editor >
Templates. You can also start typing "Code templates" in the top text
box to find the menu.
- For the formatter, go to
Java > Code Style
> Formatter. You can also start typing "Formatter" in the top text
box to find the menu
Figure 20: Eclipse Preferences for Code Templates
Here are a few other key code templates:
- There are several
for code templates that will
create a for-loop for you
foreach template will search upward from your
current location and find the nearest
Iterable type and
create an enhanced for-loop over it.
test code template will create a JUnit 3 test
method for you
/** before a method will generate Javadoc,
inferring the parameters to your method
The Eclipse editor knows a lot of common errors. By storing
patterns of common errors, Eclipse can give you a "Quick Fix" for your
- In your
HelloWorld program, type a print
statement below your first print statement (use the code template!).
Figure 21: A second print statement
- Now let's type in a variable to print out, say
Save (Ctrl + S) after typing in the variable name.
Figure 22: Compile Error!
- Looks like we've got a compile error. (Remember that saving
automatically compiles our code.) You can tell this in several ways:
HOWEVER! You may also notice a little lightbulb on the error icon: . This means that Eclipse has an
idea for how to fix your problem. In our case, all we need to do is
declare a new String variable. Let's let Eclipse do this for us.
- There's a red squiggly under our new variable
- There's a little red X along the left-hand side of editor:
Problems view at the bottom shows an error:
Figure 23: The Problems View
- With your text cursor anywhere inside the red squiggly, hit
+ 1 (that's the number ONE; Mac users, try
Apple + 1). Up
pops the quick-fix menu:
Figure 24: The Quick Fix Menu
- Notice all of the options here. Select the first option by
Enter. Eclipse will guess at what your variable
type is by doing
char (it may be slightly different in
different versions of Eclipse).
Figure 25: Creating a new variable
- Since Eclipse can't read our minds, it couldn't have known
that we wanted a String. That's okay, change your variable type to a
Don't forget! You don't even have to type all of String - use
Ctrl + Space). The following figure shows
what this looks like.
Figure 26: Changing to a String with Auto-complete
- Looks like we've got another problem... our String variable is
not initalized! Initialize the string in your usual way: Use Quick-fix
again to remedy this.
Figure 27: Initializing our variable.
- Let's do one more. Suppose we want to create a class that says
hello world in another language. Instead of creating our class the old
way, let's just use it. Put the following code at the bottom of your
SpanishHelloWorld spanishHW = new SpanishHelloWorld();
You should get some compile errors from this:
Figure 28: Using without defining a new class
- Put your text cursor on the squiggly and hit
+ 1 again for quick fix. Select Create Class.
Figure 29: Using Quick Fix to create a new class
- A new Create Class window will pop up. Recognize it? The
defaults are okay - just hit
Finish (or Enter)
- You are now in your new
class. Switch back to the
HelloWorld.java editor (Ctrl +
F6 is the keyboard shortcut, Mac users try
Notice that we've still got another compile error... our method was
never declared. Use Quick Fix again to create the new method.
Figure 30: Making a method using Quick Fix
- Now we can use your
sysout template to print it
out in Spanish!
Figure 31: Finishing it off
- Run your program again to make sure it works. You should get
The Eclipse Quick Fix tool can be very powerful, but also limited to
very common and easily-solvable problems. The Quick Fix will only work
on compile errors, not
logic errors, for example. Quick Fix may
not always fix your code exactly how you want to, so it's not a good
idea to completely rely on this feature (it's only meant to save you
time, anyway). Among the other fixes are finding an import, creating a
class/method/variable, public/private access, fixing parameters in a
method, and many others.
Using Quick Fix for "Error-Driven Development"
One way to use Quick Fix is to first write your code ignoring all of
your declarations. If you want to call a class you haven't written yet,
just use it how you plan on using it, then use quick fix to do all of
the declarations. It takes some getting used to, but allows you to write
quickly in the end because you don't have to do so
many declarations. I find myself writing code in this way because I can
simply write what I am currently thinking about without having to stop
and deal with details that can be Quick-fixed anyway. You might find
that this way of writing code is not so different from writing
5.0 Code Navigation Tools
Eclipse provides some tricks for traversing through code. In this
section we'll cover:
- Going to the declaration by clicking
- Package explorer
- Go to your
HelloWorld.java program. Hold down the
Control key and hover your mouse over the declaration of
SpanishHelloWorld should turn into a hyperlink.
Figure 31: Turning class declarations in to hyperlinks
Control still held down, click on the
hyperlink (Mac users try the
Apple key). This will take
you to the
- In general, if you
Ctrl + Click on something,
you'll go to where it is defined. So clicking on the
method call will go to that method declaration. This can save you a lot
of time of finding where something is defined. If you cannot get it to
work, make sure you've got the following things:
- Make sure the current file is saved. Sometimes compilation
will refresh what Eclipse knows about each class.
- Make sure that you have all of your compile errors fixed.
- Sometimes you need to hold down the
for a half-beat before the hyperlinks show up.
- If you hover too long, the hyperlinks go away.
- The Package Explorer can
also be a very helpful view for managing large projects. Notice that we
have our two classes in the Package Explorer already.
Figure 32: Our Package Explorer
- Use the plus sign to expand each
element of our
src folder. Here we see all of methods,
instance variables, and static variables of our class.
Figure 33: Expanding each part of our classes
There are a multitude of other resources related to Eclipse out
there. Here are just a few:
Back to Software Engineering Tutorials
© 2008 North
Carolina State University, Laurie Williams and Andy Meneely
Last Updated: September 25, 2008
Questions? Comments? Contact our mailing list: