Introduction to Eclipse


Introduction

This Tutorial will introduce you to Eclipse, a full-featured and very versatile Integrated Development Environment. During the assignments and labs in this course you will be using Eclipse extensively to develop Java programs.

By the end of this Tutorial you should be able to:


1. Getting Started

1.1 Directories

All of your work done in each lab and assignment throughout the course should be stored in your account on the undergrad machines.

In a similar way, each lab and assignment will be stored in an appropriate directory (lab1, a3...), under ~/cs211.

1.2 Run Eclipse

This tutorial assumes you are working in CICSR 005, where all of your labs will be held. If you are working from home, you will need an up-to-date copy of both Java and Eclipse.

In this class, we will be using a relatively new version of Java, version 5.0 nicknamed 'Tiger'. Java is available for free from Java's download site. Note that Java 5.0 is equivalent to Java 5. Be sure to download the JDK (Java Development Kit), as opposed to the JRE (Java Runtime Environment). The JRE does not include the javac compiler, which is essential to this course!

Eclipse is also available for free from Eclipse's download site. Note that only versions of Eclipse 3.1 support Java 5.0. Eclipse 3.0 and lower are not sufficient. The latest stable milestone build, version 3.1M1, is installed in the lab; we recommend using that version at home. Also note that 5.0 support is not yet complete, so some features of the language will not yet work within Eclipse, such as Autoboxing and Enumerations, but you won't have to worry about this just yet.

Installation of both Java and Eclipse is straightforward, and well documented in their respective download packages. Once you have both installed, you are ready to finish this tutorial.

If you are working from the Undergraduate machines on campus, be sure to run the correct version of Eclipse. To do so, on the fvwm toolbar, do not use the Eclipse Button, as that will run version 3.0. Instead, click on the adjacent Java 5 Button, and from the pop up menu select Use Java 5 in Eclipse. This will launch Eclipse.

1.3 Setting a Workspace

Eclipse uses a workspace to store your configuration settings and all of the files you are currently working on. In CPSC 211 you will use a different workspace for each lab and assignment. Setting up each of these workspaces will be detailed in the respective lab and assignment. For this tutorial, feel free to use the default workspace.

1.4 Adding Java 5.0 Support to Eclipse

When you first run Eclipse 3.1, you need to check to find out which Java version it uses.  If it is using Java 1.4 or lower ,  you must tell it to use 5.0 instead. To do so, click Window -> Preferences. In the Preferences window, select Java -> Compiler then select the Compliance and Classfiles tab. Under Compiler compliance level, select 5.0.

1.5 Configuring the code formatter.

There are many different ways to format code. One way to format the opening and closing braces for a method is as follows:

int getValue() {
return value;
}

However, your textbook uses the following format:

int getValue()
{
   return value;
}

The Eclipse code formatter has a very long list of options. Fortunately, they can all be configured in one convenient step. Start by downloading the file CPSC211CodeFormat.xml to your cs211 directory. Back in the Preferences window in Eclipse:


2. Perspectives & Views


3. Hello World Example

Let's go through a simple example that will illustrate some major features of Eclipse, and demonstrate how Java projects are created and managed.

3.1 Creating a Project

3.2 Creating a Package

3.3 Creating a Class

3.4 Coding, Saving & Compiling

3.5 Running the Program

  1. In the Package Exlporer, select HelloWorldClass.
  2. Select Run -> Run..., or right-click on HelloWorldClass and select Run -> Run.... This will allow you to create a launch configuration, which tells Eclipse how to run your application.
  3. Choose the type of launch configuration to create. HelloWorld is a standard Java Application, so double click on Java Application.
  4. The Project field should contain HellowWorldProject, while the Main Class field should contain HelloWorldClass. If not, enter either one in.
  5. Click Run to execute this new launch configuration.
  6. Run Configuration

  7. The Console view handles the Standard I/O, so after running the program, Hello World, appears in the Console window.
  8. Add the line System.out.println(args[0]); after the line System.out.println("Hello World");.  Our program will now print out the first command line argument after Hello World. Your code should look something like this:
    public class HelloWorldClass
    {
        public static void main(String[] args)
        {
            System.out.println("Hello World!");
            System.out.println(args[0]);
        }
    }
  9. To run our program with command line arguments:
    1. Click Run -> Run... to bring up the launch configuration window.
    2. Under Java Application, select HelloWorldClass.
    3. Click the Arguments tab, in the Program Arguments field enter Goodbye and click Run.  This is equivalent to typing java HelloWorldClass Goodbye in the command prompt.

      Run with Arguments

    4. The console should display:
      Hello World
      Goodbye
      • Tip: To run the file currently open in the text editor without any special settings (e.g. command line arguments), select Run -> Run As -> Java Application.
      • Tip: To rerun the last application you ran, click the Run button, , on the top horizontal toolbar (buttons to create new projects, packages, and classes are also on the toolbar).  Buttons on this toolbar differ among perspectives.
      • Tip: To run an existing launch configuration, click on the down arrow next to the Run button and selec the desired configuration.
That's all this tutorial has for you. Any other features of Eclipse you may need for an assignment or lab will be detailed there.