Getting Started with Eclipse 3.4 for Beginning Programmers
Andy Meneely
CSC 116 - Introduction to
Java Programming
Department of Computer Science
North Carolina State University
Back to Software Engineering Tutorials
| 1.0 | Introducing Eclipse |
| 2.0 | Installing and Starting Eclipse |
| 3.0 | Our First Eclipse Project |
| 4.0 | Code Editing Tools |
| 5.0 | Navigating Through Code |
| 6.0 | Resources |
Target Audience: this tutorial is geared toward the beginning Java programmer who has some experience with Java programming from the command-line.
When you first learned how to write Java programs, you probably
started by writing in a text file, compiling using the javac
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:
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:
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.
Download Eclipse.Eclipse for Java Developers link on the
page. Click on the link for your operating system along the right-hand
side.

c:\eclipse-3.4.
Remember this location - we will refer to this location as <YOUR
INSTALL LOCATION>.<YOUR INSTALL
LOCATION>/eclipse/Eclipse.exeWhen you first start Eclipse, you will be asked to select your workspace. Like this:

The Eclipse workspace is a permanent location for all of your work.
c:\workspace.The first time you start with a fresh workspace, you will be
shown a Welcome view. Close the Welcome view
according to the following figure

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 Window >
Reset Perspective. If you are not in the Java perspective, use Window
> Open Perspective > Other... > Java (NOT Java Browsing!)

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 HelloWorld.
Package Explorer (
) and select New >
Java Project as shown in Figure 4.

HelloWorld as the name of your
project. The other default settings are okay.

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.

src folder in your project. This is
where we'll put all of our Java classes. So let's create a Java class
called HelloWorld.java. Right-click on the src
folder, and select New > Class.HelloWorld. These do not
need to be the same.

Name box type HelloWorld for
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.

Finish. Eclipse generated a file called HelloWorld.java,
placed stubs for comments and the main method. A Java
editor has also been opened.

System.out.println("Hello, World"); . Press the
Save (
) button at the top (or hit Crtl+S).

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 HelloWorld.java
in the Package Explorer and select Run as...
> Java Application.

Console
view (
). We just ran our first Java
program in Eclipse!!

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:
HelloWorld
class. Place this at the top of your main method and then
hit Save
: String hello = "Hello, World";
hello variable. Delete the string literal in your
print call, and type h (JUST the letter h!). Like this:
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
looks like:

hello is at the top of the
list. Hit Enter to add the rest of the variable's name.Ctrl + F11.Apple + F11 for re-running
a program.hello from the print call and
re-run this procedure without looking at the instructions - practice
makes perfect!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 about
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 Enter
key.
HelloWorld program, delete the main
method entirely.

main
and hit Crtl+Space.

main method. Hit Enter
to select it.main method! Note that the
text cursor lands in the middle of the method so you can just start
typing code.

sysout.
Invoke the sysout template the same way you did the
main template. (If you type all of sysout before hitting Ctrl
+ space, the menu doesn't pop up because there's only one option!).System.out.println(); call
from the code template, type your string literal of "Hello,
World!" in it. Re-run your program to ensure that it's correct.

Ctrl + Shift + F (or go
to the Source > Format in the menu; Mac users try Apple
+ Shift + F).

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 templatesysout code templateWindow > Preferences (Mac Users
it's Eclipse > Preferences.
Java > Editor >
Templates. You can also start typing "Code templates" in the top text
box to find the menu. Java > Code Style
> Formatter. You can also start typing "Formatter" in the top text
box to find the menu
for code templates that will
create a for-loop for youforeach 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 methodThe Eclipse editor knows a lot of common errors. By storing patterns of common errors, Eclipse can give you a "Quick Fix" for your problem.
HelloWorld program, type a print
statement below your first print statement (use the code template!).
Like this:

hello.
Hit Save (Ctrl + S) after typing in the variable name.

Problems view at the bottom shows an error:

Ctrl
+ 1 (that's the number ONE; Mac users, try Apple + 1). Up
pops the quick-fix menu:

Enter. Eclipse will guess at what your variable
type is by doing char[] (it may be slightly different in
different versions of Eclipse).

String.
Don't forget! You don't even have to type all of String - use
Auto-complete (Ctrl + Space). The following figure shows
what this looks like.


SpanishHelloWorld spanishHW = new SpanishHelloWorld(); spanishHW.sayHello();You should get some compile errors from this:

Ctrl
+ 1 again for quick fix. Select Create Class.

Finish (or Enter)SpanishHelloWorld.java
class. Switch back to the HelloWorld.java editor (Ctrl +
F6 is the keyboard shortcut, Mac users try Fn+Apple+F6).
Notice that we've still got another compile error... our method was
never declared. Use Quick Fix again to create the new method.

sysout template to print it
out in Spanish!

Hello, World! Another Hello! Hola, Mundo!
Eclipse provides some tricks for traversing through code. In this section we'll cover:
HelloWorld.java program. Hold down the
Control key and hover your mouse over the declaration of SpanishHelloWorld.
The SpanishHelloWorld should turn into a hyperlink.

Control still held down, click on the SpanishHelloWorld
hyperlink (Mac users try the Apple key). This will take
you to the SpanishHelloWorld class.Ctrl + Click on something,
you'll go to where it is defined. So clicking on the sayHello()
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:
Control key
for a half-beat before the hyperlinks show up.
can
also be a very helpful view for managing large projects. Notice that we
have our two classes in the Package Explorer already.

to expand each
element of our src folder. Here we see all of methods,
instance variables, and static variables of our class.

There are a multitude of other resources related to Eclipse out there. Here are just a few: