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


0.0 Contents
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.

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 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:

Top | Contents

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.

  1. Go to http://eclipse.org and click on Download Eclipse.
  2. Find the Eclipse for Java Developers link on the page. Click on the link for your operating system along the right-hand side.

    Figure 1: Downloading the Eclipse for Java Developers
  3. Select a mirror and download the file. Make sure that you are downloading a zip file.
  4. Once the zip file is downloaded, unzip the file to a location on your computer. I usually choose something like c:\eclipse-3.4. Remember this location - we will refer to this location as <YOUR INSTALL LOCATION>.
  5. Start Eclipse by executing <YOUR INSTALL LOCATION>/eclipse/Eclipse.exe

Starting Eclipse

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 your work.

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


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


Figure 3: The Java Perspective
Top | Contents

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 HelloWorld.

  1. Right click in the Package Explorer () and select New > Java Project as shown in Figure 4.

    Figure 4: Creating a new Java Project
  2. 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
  3. Select 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
  4. 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 called HelloWorld.java. Right-click on the src 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
  5. In the 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.

    Figure 8: Naming and Configuring the New Class
  6. Hit Finish. Eclipse generated a file called HelloWorld.java, placed stubs for comments and the main method. A Java editor has also been opened.

    Figure 9: Editing our new class
  7. Place the print statement for the string literal "Hello, World": System.out.println("Hello, World"); . Press the Save () button at the top (or hit Crtl+S).

    Figure 10: Adding our print statement and saving.
  8. 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 HelloWorld.java in the Package Explorer and select Run as... > Java Application.

    Figure 11: Running a Java program
  9. The output to your program gets printed out to the Eclipse Console view (). We just ran our first Java program in Eclipse!!

    Figure 12: Viewing console output
Top | Contents

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:

Auto-Complete

  1. First, let's define a variable in our HelloWorld class. Place this at the top of your main method and then hit Save :
    String hello = "Hello, World";
    	
  2. Now let's change our print statement to print out the contents of our 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
  3. 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 looks like:

    Figure 14: Auto-complete menu
  4. This is the auto-complete menu. Note that our previously-defined variable hello is at the top of the list. Hit Enter to add the rest of the variable's name.
  5. 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 a program.
  6. Remove the variable hello from the print call and re-run this procedure without looking at the instructions - practice makes perfect!
Hint
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 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.

Code Templates and Formatter

  1. In our HelloWorld program, delete the main method entirely.

    Figure 15: Start with an empty class.
  2. With your text cursor in the program, start typing the word main and hit Crtl+Space.

    Figure 16: The Main method code template
  3. There should be a code template (with the following icon: ) for the main method. Hit Enter to select it.
  4. Voila! There's our main method! Note that the text cursor lands in the middle of the method so you can just start typing code.


    Figure 17: The Main Method
  5. Eclipse has another helpful code template called 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!).
  6. Now that we have our 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.

    Figure 18: The full Hello, World program
  7. 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 to the Source > Format in the menu; Mac users try Apple + Shift + F).

    Figure 19: Formatted HelloWorld
  8. 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:
    • The main method code template
    • The sysout code template
    • The code formatter
  9. If you want to add/edit code templates or the way your code gets formatted, go to Window > Preferences (Mac Users it's 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:

Quick Fix

The Eclipse editor knows a lot of common errors. By storing patterns of common errors, Eclipse can give you a "Quick Fix" for your problem.

  1. In your HelloWorld program, type a print statement below your first print statement (use the code template!). Like this:

    Figure 21: A second print statement
  2. Now let's type in a variable to print out, say hello. Hit Save (Ctrl + S) after typing in the variable name.

    Figure 22: Compile Error!
  3. Looks like we've got a compile error. (Remember that saving automatically compiles our code.) You can tell this in several ways:
    • There's a red squiggly under our new variable
    • There's a little red X along the left-hand side of editor:
    • The Problems view at the bottom shows an error:

      Figure 23: The Problems View
    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.
  4. With your text cursor anywhere inside the red squiggly, hit Ctrl + 1 (that's the number ONE; Mac users, try Apple + 1). Up pops the quick-fix menu:

    Figure 24: The Quick Fix Menu
  5. Notice all of the options here. Select the first option by hitting 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
  6. 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 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.

    Figure 26: Changing to a String with Auto-complete
  7. 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.
  8. 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 main method.
    SpanishHelloWorld spanishHW = new SpanishHelloWorld();	
    spanishHW.sayHello();
    	
    You should get some compile errors from this:

    Figure 28: Using without defining a new class
  9. Put your text cursor on the squiggly and hit Ctrl + 1 again for quick fix. Select Create Class.

    Figure 29: Using Quick Fix to create a new class
  10. A new Create Class window will pop up. Recognize it? The defaults are okay - just hit Finish (or Enter)
  11. You are now in your new 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.

    Figure 30: Making a method using Quick Fix
  12. Now we can use your sysout template to print it out in Spanish!

    Figure 31: Finishing it off
  13. Run your program again to make sure it works. You should get this output:
    Hello, World!
    Another Hello!
    Hola, Mundo!
    	
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 code very 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 pseudocode first!
Top | Contents

5.0 Code Navigation Tools

Eclipse provides some tricks for traversing through code. In this section we'll cover:

  1. Go to your HelloWorld.java program. Hold down the Control key and hover your mouse over the declaration of SpanishHelloWorld. The SpanishHelloWorld should turn into a hyperlink.

    Figure 31: Turning class declarations in to hyperlinks
  2. With Control still held down, click on the SpanishHelloWorld hyperlink (Mac users try the Apple key). This will take you to the SpanishHelloWorld class.
  3. In general, if you 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:
    • 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 Control key for a half-beat before the hyperlinks show up.
    • If you hover too long, the hyperlinks go away.
  4. 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
  5. 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
Top | Contents

6.0 Resources

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

Top | Contents


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: se_tutorials@lists.ncsu.edu