CPSC 225 Intermediate Programming Spring 2022

Using Eclipse

Introduction

The purpose of this lab is to introduce you to the Eclipse IDE. IDEs (or integrated development environments) combine the basic tools of software development - an editor and a compiler - with other useful things such as automation of the build process, a debugger, code-generation tools, and support for quickly browsing and navigating the files in a large software project. Programming can certainly be done without an IDE, but a good IDE can help streamline the process and make development faster and easier.

We will be using Eclipse, an open-source IDE originally developed by IBM. Learning to use an IDE will make it easier to develop and manage large programs, and is a useful "real world" job skill. Eclipse, in particular, is nice because it is freely available and supports multiple languages (so you don't have to switch IDEs if you switch programming languages). It is also a real piece of software, used in actual software development environments - it's not a simplified teaching tool.

This lab will introduce some of the fundamental concepts in Eclipse as well as some of its most useful features. There's a lot more Eclipse can do (though not all of it is relevant at this point) - feel free to explore.


Eclipse Concepts

A project contains all of the files associated with a single program. A workspace contains a collection of projects along with preferences and settings (like code formatting conventions) common to those projects. This organization allows you to configure Eclipse in different ways simultaneously without having to repeat configuration steps when settings are common to many different projects.

When you run Eclipse, what you see in the Eclipse window is the workbench. The workbench window contains various editors and views allowing you to see and interact with your program. A particular configuration of editors and views is known as a perspective. We will primarily use the Java perspective in its default configuration, though it is possible to customize any perspective if you wish.


Initial Configuration

Directory Setup

You will need a workspace directory for your Eclipse projects.

You can create multiple workspace directories if you use Eclipse for different things e.g. you might have one workspace for your CPSC 225 projects and another for projects for another course. Having separate workspaces allows you to keep unrelated projects separate from each other, and also facilitates applying different settings (such as code-formatting conventions) to different sets of projects.

Creating a Launcher Icon

Since Eclipse is something you will use often, you may want to make a shortcut for it:

Eclipse Setup

The final setup is to configure Eclipse to use your workspace directory, and to use the code-formatting and other conventions we'll use for this course.

The first time you start Eclipse, you will be prompted to specify a workspace directory. All of the programs you write for this course should go into the ~/cs225/workspace directory that you just created.

If you are starting Eclipse for the first time with a new workspace, you should see a welcome screen like the following:

Eclipse welcome screen

Close the welcome screen to get to the workbench display shown below. (If you have previously started Eclipse with a particular workspace, it will go straight to the workbench display, bypassing the welcome screen.)

Eclipse workbench

One useful feature of an IDE is the ability of the IDE to handle some of the grunt work for you, such as auto-formatting and auto-generating code. Since there are different conventions, these settings are user-configurable. The rest of this section deals with specifying the configuration that we'll be using for this course. Note that all of these settings are workspace-specific - if you install Eclipse on your own computer (and create a new workspace there), you will need to repeat this configuration there. Similarly, if you create another workspace for another course, you will also need to repeat this configuration (or specify a different configuration). You do not, however, need to redo these steps each time you create a new project within an existing workspace.


Using Eclipse

Java development work is done in the Java perspective. If your Eclipse window doesn't look like the following, ask for help getting there.

Eclipse workbench

Hello World

Time to create a program! There are five basic steps: switching to the Java perspective (usually you'll already be there), creating a project to contain your program's files, creating a main program, writing some code, and running the program.

Syntax Errors

As you type code, Eclipse tries to be helpful - it pops up warnings and suggestions for code completion, and puts all sorts of little markers and icons in and next to your code indicating syntax errors and other things. To further explore how Eclipse can help you find and fix syntax errors, do the following to your (working) hello world program:

As you make these errors (deliberately), notice that a squiggly red underline marks the location of a potential problem, red X icons in the left margin of the editor tab mark lines with errors, and several red rectangles in the right margin of the editor tab indicate the presence of errors within the file as a whole.

You should notice that red Xs indicating the presence of errors now appear next to the file and the project in the Package Explorer.

Now, let Eclipse help you fix the errors:

Packages

Packages are an important way of organizing the potentially many classes within a program and Eclipse is eager to put new classes into a package. For now, however, using the default package is fine. (That's why you deleted the entry in the "Package" box when you created HelloWorld.)

If HelloWorld.java isn't listed under "(default package)" in the Package Explorer:

Formatting

Another handy feature in Eclipse is auto-formatting - so there is no excuse for handing in poorly-formatted code!

Frequently auto-formatting your files helps with readability and can help reveal bracket-nesting errors.

Importing Files and Using Objects

You will typically make use of objects of many different types in a program, and the class definitions for each of the objects you want to use must be available to both the compiler (javac) and the runtime system (java). By default, the system looks for any classes that aren't part of the standard Java libraries in the same directory as the rest of your program - which, for Eclipse, means they are included in the same project.

Often you will be creating classes from scratch, but Eclipse also allows you to import existing files into a project. To do this:

Importing files like this copies them to your project directory - any changes you make will not affect the original copy.

Finally, modify the main program to make use of the Person class:

Testing Your JavaFX Setup

JavaFX is used for creating GUI programs. Actually programming with JavaFX won't be covered until later in the semester, but code may be provided as part of labs or programming assignments that uses JavaFX so you need to be able to run JavaFX programs. This takes a bit of setup because JavaFX is not distributed with the rest of the Java JDK.

Earlier in the lab you configured Eclipse to be able to find the JavaFX libraries on the system. Now you should test your configuration to make sure it works:

If there are compiler errors after you import the file or runtime errors when you try to run the program, there is something wrong with your JavaFX setup. Double-check the last two steps in the Eclipse setup section above (the two that have to do with JavaFX) and if there are still errors, ask for assistance. If everything is well, running the program should pop up a window with several buttons that you can click.


Exploring and Getting Help

Eclipse has an extensive online help system available through the "Help" menu. "Help"→"Welcome" is a good place to start for an introduction to Eclipse; "Help Contents" and "Search" are useful if there is something in particular you are looking for help with. "Dynamic Help" can also be handy - it brings up help topics associated with what you are currently doing.

Stop! You are done! The rest of this section is optional - you can check it out if you are already comfortable with Eclipse and want to become more of a power user, but it isn't necessary at this point.

Exploring

Eclipse is a powerful tool, and does much more than has been described so far or that will be covered in this course. Feel free to explore and to experiment, either now or during the semester. A few things of particular interest: