CPSC 225, Spring 2019
Running Eclipse

This supplement to Lab 1 explains how to use the Eclipse IDE (Integrated Development Environment). Although you are not required to use Eclipse, you are strongly encouraged to use it and to learn how to use some of its more advanced features.

Running Eclipse

This section introduces you to the Eclipse IDE. This section just describes Eclipse; the next section will tell you how to use it for a programming project. Eclipse is a widely used professional environment for program development. Note that Eclipse and IDEs in general are also discussed in Section 2.6 of the textbook. Eclipse is already installed on the Linux computers that you will use during the lab. You can access it in the "Programming" sub-menu of the application menu, under the name "Eclipse Oxygen". You might want to add an "Eclipse" button to the panel at the top of the screen to make it easier to get to. To do that, simply right-click the "Eclipse Oxygen" item in the menu, and select "Add to Panel".

Although it is not required for the course, I encourage you to consider running Eclipse on your own computer as well. It is available for Linux, Windows, and Mac OS. You can download the most recent version of Eclipse from the Eclipse downloads page at https://www.eclipse.org/downloads/packages/. You should get the "Eclipse IDE for Java Developers". Note that you need to have Java on your computer before you install Eclipse, since Eclipse is itself written in Java. If you would like some help installing Java or Eclipse on your computer, ask for it.


Eclipse organizes your programming projects into "workspaces." A workspace is really just a directory, and a project is a subdirectory inside that directory. A project holds the files relevant to a given programming project, such as Java source code files, compiled class files, images, and data files. You should let Eclipse have complete control over the workspace directories, and you should not directly change any files that are stored in them; changes should be made through the Eclipse window. It is possible to have as many workspaces as you want, but for this course, you will probably keep all of your projects in the same workspace. (In fact, you can keep all the programs that you ever write in one workspace, if you want.)

When Eclipse starts up for the first time, it asks you to select a workspace. (There is a box that you can check if you don't want to be asked to select a workspace each time you run Eclipse.) By default, the name of the workspace will be "workspace", but I usually change the default name. If you expect to devote a workspace to CS225 only, for example, you might name it "workspace-cs225".

If you already have an Eclipse workspace and want to create a new workspace for CS225, you can just enter the new workspace name when Eclipse starts up. Or, if Eclipse is already running, you can use the command "File" / "Switch Workspace" / "Other...", and you will have the opportunity to enter a new workspace name.

When the Eclipse window first opens, it will be filled with a "Welcome" screen. The icons on this screen link to a large amount of information about Eclipse. You can browse through this information later, if you like, but for now, just close the Welcome screen by clicking the "×" next to the word "Welcome" near the top left corner of the window. To avoid seeing the Welcome screen every time you start Eclipse, uncheck the box labeled "Always show Welcome at startup" at the bottom left corner of the Welcome screen.

Eclipse uses the terms view and perspective to describe the way that information is organized and presented in its window. A "perspective" refers to the entire contents of the Eclipse window. A "view" is one of the panels in a "perspective." The window typically contains several views. Each view contains a certain type of information. All the views that are visible in the window constitute a perspective. A perspective is meant to contain all the views that are used in some particular activity. For now, you will just use the Java perspective, which is organized for Java programming. Later, you will learn about the Debug perspective, which is for debugging programs. Each perspective is highly customizable. You can add and delete views and move them around. If you ever delete a view accidentally, you can get it back by selecting it from the "Show View" submenu in the "Window" menu. If you mess up a perspective, you can get back the original, default setup with the "Reset Perspective" command in the "Perspective" submenu of the "Window" menu.

After you have closed the Welcome screen, the window will show the Java perspective. Initially, all the views are empty. Here is what the Java perspective might look like after a little work has been done:

Eclipse Window showing Java Perspective

In this window, I have closed a couple of the views that I don't use: the "Task List" and "Outline" views, which originally appeared on the right edge of the window. Remember that a view can be closed by clicking the small "×" next to the name of the view, and it can be reopened using the "Window" / "Show View" menu.

The "Package Explorer" view, on the left, is central to much of the work that you do in Eclipse. It contains a list of your programming projects and the Java files and resources that are contained in those projects. In the above picture, there is just one project, named Lab1. Clicking on the small triangle (or plus sign) next to the project name will show/hide the resources contained in the project. In a new project, there will be a directory named src where the source files for the project will be stored. Inside the src folder, you will see Java files organized into packages. In many cases, we will only use the "default package," as shown in this example, but sometimes you will need to work with other packages. (See Section 4.6 in the textbook if you are unfamiliar with the idea of packages.)

The lower right section of the window contains several views. In the picture, the "Console" view is showing. To see one of the other views, such as "Problems" or "Javadoc", just click on the tab that contains the name of the view. Sometimes, another view will pop up automatically in this area to show the output of some command. When you run a program, standard input and output are done in the "Console" view. Errors and warnings from the Java compiler are displayed in the "Problems" view.

The central area of the window is occupied by editor views. Here, I've created a Java file, Tabulate.java, and have opened it for editing. Another source file, named Expr.java, has also been opened and is in another tab.

The view of Tabulate.java shows several of the nifty features of the Java editor. The source code that you type is checked for syntax errors as you type. Errors are marked with small red carets at the bottom of the line. The error is also marked by a red rectangle in the right margin of the editor and sometimes by an icon in the left margin of the editor; if you hover your mouse over any error or error marker, you see an error message for the error. For the first error in the picture, for example, you would be told that a semicolon is missing. In the third line of main(), the word "prin" is underlined with error markers. (It's a misspelling of "print".) This error has an error light bulb () in the left margin of the editor. The light bulb means that Eclipse is not just telling you about the error but is offering you some ideas for fixing it. If you click the light bulb, you get a list of actions that can be taken to fix the problem. You will also see the list if you hover the mouse over the underlined error itself. For the example, here is what I get when I hover the mouse over the incorrect word "prin":

change to printt(), and other options

Clicking on an action, such as "Change to print()", will apply that option automatically. Sometimes, you will see a warning light bulb (). A warning indicates a potential problem, but not an error. Warnings will not prevent the source code from being compiled successfully.

In fact, Eclipse might be a little too enthusiastic in marking warnings and errors! You do not have to fix every warning. And you do not have to fix every error as soon as it appears. In fact, it's impossible to do so. In some cases the error will go away by itself after you've typed in more of your program. And remember that the fix for a programming error does not always go at the location of the error; sometimes the problem is elsewhere in the file. Furthermore, Eclipse's error system is only effective if you routinely get most of your program correct in the first place — don't expect Eclipse to make solid Java programming skills unnecessary!

In fact, many of the fixes that Eclipse will offer will do things to your code that you won't understand. Not every fix is a good idea! Don't let Eclipse fix your code unless you understand what it wants to do and why!

Note that Eclipse will even spell-check your comments. It will mark a misspelled word in a comment with a red underline. If you hover the mouse over a misspelled word, you will get a list of possible corrections. Click an item in this list to apply the correction to the word.

An Eclipse Project for Lab 1

It's time for you to start using Eclipse. Start up Eclipse, as described above. Close the "Welcome" screen (and probably the "Task List" and "Outline" views as well). You will create a project that you can use for your Lab 1 work.

To create your project, right-click in the "Project Explorer" pane on the left edge of the Eclipse window. In the pop-up menu that appears, go to the "New" submenu, and select "Java Project." (Alterntively, select "New" / "Java Project" from the "File" menu.) A "New Project" wizard will pop up. All you have to do is enter a name for the project, in the box labeled "Project Name". Enter a name, such as lab1, for the project. After entering the project name, click the "Finish" button. The project will be added to "Project Explorer", and a directory of the same name will be created in your Eclipse workspace directory.

In general, you will need to create some new Java classes in any project, but often you will also import some existing files into your project. For the first lab, you should import the files from the directory /classes/cs225/lab1-files into your project. Here is how to do that...

To import the files: Click the triangle next to the project name, "Lab1", so that you can see the folder labeled "src". Open a file browser window (for example by double-clicking the "Home" icon on your desktop), and browse to the folder /classes/cs225/lab1-files. You should see the files ArithemeticProblem.java, WordList.java, unsorted_words.txt, and TextIO.java. Drag at least the first three files from the file browser window onto the "src" folder in the Eclipse window. If you plan to use TextIO for user input, you should also drag TextIO.java into the src folder. You will be asked whether you want to "Copy" or "Link" the files. Choose "Copy." Copies of the files will be added to the project. (Alternatively, you can "Copy" the files from the file browser, then right-click the src folder and "Paste" the files.) If you click the triangle next to "src", you should find unsorted_words.txt directly in the "src" folder, while the three Java files have been placed into the "(default package)". To open a file in the Eclipse editor, just double-click its name in the Package Explorer view.

To create a new Java class file in your project: Right-click the "default package" in the "Package Explorer" view. In the pop-up menu, go the "New" submenu again, and select "Class":

menus for creating a class

A class creation dialog box will pop up. Again, you just have to fill in the name of the class, and click the "Finish" button. Note that you have to enter the name of the class, not the name of the file, so don't try to add a ".java" extension to the name. The name must be a valid Java class name. For example, to create the class for the first programming assignment in Lab 1, use "ArithemeticQuiz" as the class name. Furthermore, a Java editor will open, showing you the initial contents of the file. As you will see in the editor window, Eclipse has already added the declaration of the class. All you have to do is fill it in! Note that you can close the editor in the usual way by clicking the little ×. To open the file again, double-click its name in the "Project Explorer."

Eclipse often has alternative ways of doing things. Another way to create a class is with a button in the toolbar. Look for the group of two small buttons at the top of the Eclipse window:

buttons for creating packages and classes

Click on the left button in this group to create a new Java package. Click the right button to create a new class. (When you create a class using the button, you should first click in the Package Explorer to select the location of the class that you are creating. Otherwise, you'll have to enter the location by hand in the class creation dialog box. Jave might enter a package name into the class creation dialog box; if you want the class to go into the default package, you should make sure that the "Package" input field is empty.)


Now, to create and run a program. As an example, add a main() routine, such as the following, to the ArithemeticQuiz class:

     
public static void main(String[] args) {
   System.out.println("Welcome to my arithmetic quiz.");
}

There is no separate compilation step when running a program in Eclipse. The program is compiled automatically. To run a program, you can right-click either on the name of the class in the "Project Explorer" or on the editor window that contains the program. In the pop-up menu, go to the "Run As" submenu, and select "Java Application" from the submenu. The program will start. The output from any print statement will appear in the "Console" view in the bottom right area of the Eclipse window; if that view is not already visible, it will open as soon as the program does some output. If the program reads input from the user, you must first click the Console view before you can type into the Console! Once you have clicked the Console, input/output procedes just as it would on the command line.

For another way to run a program, if an editor window for the program is currently the selected view, or if its name is selected in the Project Explorer view, then you can run the program by clicking on the "Run" button (run button) in the toolbar. If you click the little black triangle to the right of the "Run" button, you'll get a list of all the programs that you have run; select a program from this list to run it.


You have surely already noticed that the Java editor in Eclipse does a certain amount of work for you. It automatically inserts indentation. When you type a "{" and press return, the matching "}" is inserted in the correct position. Furthermore, when you type a period, a list of things that can follow the period will pop up, and you can select an item from the list instead of typing it in. This is called Content Assist. You can invoke Content Assist at any time while you are typing by pressing Control-Space. If you do this in the middle of a variable name or method name, pressing Control-Space will either complete the name or offer a list of possible completions. It can be used in other ways as well. For example, if you press Control-Space after typing the "(" at the beginning of a method's parameter list, you will get information about the parameters of the method. By the way, when Content Assist pops up a list, you can get rid of the list by pressing the Escape key.

Content Assist is a good thing, but I sometimes find the way it pops up automatically while I am typing to be rather annoying. You can turn off this feature in the Eclipse Preferences. Select the "Preferences" command from the "Window" menu. In the Preferences dialog box, click the little triangle next to "Java" to open the list of Java preferences (if necessary), then click the triangle next to "Editor", and finally click on "Content Assist." In the Content Assist preferences, uncheck the box labeled "Enable Auto Activation" and click "OK".

Eclipse has a huge number of preference settings that can be used to customize the environment. Most of the default settings are OK, but there are a few that I usually change. If you want to do the same: Under "Java / Compiler / Errors/Warnings / Potential Programming Problems", change "Serializable class without serialVersionUID" from Warning to Ignore, and change "Possible accidental boolean assignment", "'Switch' case fall-through" and "Null reference" from Ignore to Warning.)


Eclipse has a lot of other useful features. We will encounter more of them as time goes on, and you can undoubtedly discover a few new ones on your own. But here are a few of the most useful: