Hands on Testing Java: Lab #1

Getting Started with Eclipse

Starting Up Eclipse

On many platforms, there should be an icon for you to double-click to start up Eclipse. If you're using Linux without an Eclipse icon, enter this at a command-line prompt:

unix-% eclipse

Either way, you'll be prompted to specify a workspace directory; use a folder that is out of the way of other programs. I would recommend something like /home/jdfrens/Eclipse/workspace (my personal preference) or /home/jdfrens/courses/cpsc185/workspace. You won't be able to use these folders themselves, so change whatever parts make the most sense (e.g., jdfrens should be replaced with your own user name; cpsc185 should be replaced with the id given to the course at your institution).

You'll have to start Eclipse this way every time you sit down to program. There's a checkbox you can check so that Eclipse does not continually ask you for your workspace directory; if you didn't check it this time, you can check it the next time you start Eclipse.

Warning: Eclipse is very particular about its workspaces. Never move or remove projects from a workspace folder unless you use Eclipse itself to do this. Many students try to move a workspace to a new location without Eclipse's help; really bad things will happen if you do this.

The Windows of Eclipse

The Eclipse Platform will start up with a variety of windows like so:

eclipse

This is a picture of a rather active Eclipse user.

Do this...
Make sure you can find the Package Explorer window, the editor, the Problems, and the Console windows.

Configuring Eclipse

In order to configure your project properly, you first have to configure Eclipse. You should have to go through these steps just once.

First, you need to create one classpath variables. The classpath is a list of directories and files where the Java compiler and JVM can find various libraries. You need to tell Eclipse where to find the support code for this lab manual (and the textbook).

Setting Classpath Variables

  1. Select the Window -> Preferences... menu item.
  2. In the left side-bar of the dialog window that pops up, select the Java -> Build Path -> Classpath Variables option.
  3. You'll see a list of classpath variables now on the right; use the New... to create a new classpath variable.
    1. Enter the name of the classpath variable in the Name field.
    2. Press the File... button to select a path to the appropriate JAR file.
    3. Press OK when you've entered information into both fields.
  4. When you are finished creating your classpath variables, hit the OK button in the Preferences window.

This is the classpath variable you need to define:

Name

Path

ANN_LIB

/home/cs/108/lib/ann.jar

Do this...
Define this classpath variables.

You won't have to do this again (unless there's another library you need for another project).

Creating the Project

Eclipse uses projects to keep different software projects separate. For your purposes, think of everything you do for this lab manual as one project. Java itself provides other ways to keep each lab exercise separate, so using one Eclipse project for all of the exercises in this lab manual makes a lot of sense.

Here's the process to create a project:

Creating a project

  1. Right-click in the Package Explorer. (Alternative: select File -> New -> Project...).
  2. Select Java Project (maybe Java -> Java Project), and hit Next>.
  3. Enter Hands on Testing Java for the Project name; and hit Next>. The other values in this dialog window should be fine.
  4. Now click on the Libraries tab.
    1. For each of the classpath variables that you created earlier:
      1. Press the Add Variable... button.
      2. Select the appropriate classpath variable, and press OK.
      If you created only one classpath variable, then add just the one.
    2. Add another library provided by Eclipse:
      1. Press the Add Library... button.
      2. Select the JUnit option, and hit the Next> button.
      3. Make sure the JUnit 3.8.1 version is selected.
      4. Press the Finish button.
    Now under the Libraries tab, you should have three entries: a JRE system library, ANN_LIB, and JUnit 3.8.1.
  5. Finally, press the Finish button (in the New Java Project dialog window).
  6. You may be asked by Eclipse if it can rearrange your windows for the "Java perspective". You want this, so say Yes.

Do this...
Create your Hands on Testing Java project using the instructions above.

You should now see a "Hands on Testing Java" in the Package Explorer on the left side of the Eclipse. Expand the project by clicking on the triangle to the left of its folder icon. You'll see a JRE ("Java Runtime Environment") entry as well as the classpath variables.

You should have to create only this one project for this lab manual. Your instructor might ask you to create other projects for other work that you do; follow these same steps, changing names as appropriate.

Answering Questions

Now you can create a file to type in your answers to the questions in the lab.

First, it's best if you have a folder to store all of these questions. Here's how you will do it:

Creating a folder in a project

  1. Select the Package Explorer.
  2. Right-click on the "Hands on Testing Java" project.
  3. Select the New -> Folder menu item to open the New Folder dialog.
  4. Make sure the project itself is selected.
  5. Enter the Folder name.
  6. Press the Finish button.

You actually need two folders, one for prelab questions and another one for exercise questions.

Do this...
Follow the directions to create two folders, Exercise Questions and Prelab Questions.

Now text files can be created in these folders for you to write your answers in.

Creating a text file

  1. Select the Package Explorer.
  2. Select and right-click on the folder which should store the new text file.
  3. Select New -> File to get the New File dialog.
  4. The folder to store the new text file should be selected in the dialog; if not, select it now.
  5. Enter in a file name with a .txt suffix.

Do this...
Create a text file in the Exercise Questions named lab01.txt. Type in the answers to all of the questions you've already accumulated for this lab.

You should also do the prelab questions.

Do this...
Create a text file in the Prelab Questions named prelab01.txt. Type in the answers to the prelab questions.

Terminology

classpath, Console, Editor, Outline, Package Explorer, Problems, project

Go back...