Eclipse Tutorial

        Step 1: Starting Eclipse (on CS Linux machines)

In order to use Eclipse on CS Linux machines you need to source /local/config/cshrc.eclipse to get it into your path. You should add "source /local/config/cshrc.eclipse" to your .cshrc file to have Eclipse in your path every time you log in to your CS account. Start Eclipse by typing "eclipse &".

The first screen that comes up after opening Eclipse is the Workspace Launcher. This screen allows you to choose where the projects you create will be saved. You may use the default, or specify a different location.

After selecting a directory you should see this window

        Step 2: Choosing a Perspective

A perspective defines the initial set and layout of views in the window. Perspectives control what appears in certain menus and toolbars. For example, a Java perspective contains the views that you would commonly use for editing Java source files, while the Debug perspective contains the views you would use for debugging Java programs. You may switch perspectives, but you need to specify an initial perspective for a workspace. To create Java programs, set the Java perspective by choosing Window, Open Perspective, Java from the main menu, as shown below:

Close the Welcome window and you will see the Eclipse user interface displayed according to the perspective:

Step 3: Creating a New Java Project

To create a project, choose File, New, Project to display New Project wizard, as shown below:

Select Java Project and click Next to display New Java Project wizard, as shown below. Type myprograms in the Project name field. Make sure that you selected the options "Create new project in workspace" and "Use project folder as root for sources and class files". Click Finish to create the project.

Step 4: Creating a Program in the Project

Now you can create a program in the project by choosing File, New, Class to display the New Java Class wizard as shown below. Type Welcome in the Name field. Check the option public static void main(String[] args).

Click Finish to generate the template for the source code Welcome.java, as shown below:

Type System.out.println("Welcome to Java"); in the main method.

Note: As you type, the code completion assistance may automatically come up to give you suggestions for completing the code. For instance, when you type a dot (.) after System and pause for a second, Eclipse displays a pop up menu with suggestions to complete the code, as shown below. You can then select the appropriate item from the menu to complete the code.

Step 5: Compiling and Running a Program

By default, your source code is dynamically compiled as you type. For example, if you forgot to type the semicolon (;) to end the statement, as shown below, you will see the red wriggly line in the editor pointing to the error:

To run the program, right-click the class in the project to display a context menu, as shown below. Choose Run as, Java Application in the context menu to run the class.

Before executing the program a window appears allowing you to save your program if it was not saved yet:

The output is displayed in the Console pane, as shown below:

Another way to run a program is to select Run, Run as, Java Application from the main menu.

Step 6: Compile and Run Java Applications from the Command Line

You can also compile and run program standalone directly from the operating system. Here are the steps to run the Welcome application from the prompt.
  1. Type cd workspace/myprograms to change the directory to workspace/myprograms.
  2. Type java Welcome to run the program. A sample run of the output looks like the following:
    % java Welcome
    Welcome to Java
    
  3. You may also compile the program using the javac command at the prompt:
    % javac Welcome.java
    

Step 7: Importing Existing Resources

Another way to add files to a project is to import them from the local file system. This is done when you already have files and want to incorporate them into the project. First, create a new project as described earlier. To practice, create a new project with the name showtime. Then, select File, Import from the main menu. The import wizard appears and gives you many different ways to import resources, we are interested in importing from the file system.

Choose File system in the window and click Next. The window that appears allows you to select a directory to retrieve files from and a directory to place files in. You may type in the name of the directory or click the Browse button to search for it. Click on the left panel to have the directory appear on the left panel as shown below. After you select the directory you need to select files which you want to retrieve from it. Click on cs272 on the left panel to get a list of files and select file ShowCurrentTime.java:

Make sure that the Into folder field has the name of the directory of the project in it (showtime). After you select the file(s) click the Finish button. To see the contents of a file you imported double click on your project name in the left panel, then double click the (default package) under your project name in the left panel, then double click on the name of the file.


Step 8: Debugging in Eclipse

The debugger utility is integrated in Eclipse. You can pinpoint bugs in your program with the help of the Eclipse debugger. The Eclipse debugger enables you to set breakpoints and execute programs line by line. As your program executes, you can watch the values stored in variables, observe which methods are being called, and know what events have occurred in the program.

To demonstrate debugging, let us use example that displays the current time. The source code for ShowCurrentTime.java can be obtained from here.
You may either import the file ShowCurrentTime.java or create a new class named ShowCurrentTime and type in the code yourself.

8.1 Setting breakpoints

You can execute a program line by line to trace it, but this is time-consuming if you are debugging a large program. Often, you know that some parts of the program work fine. It makes no sense to trace these parts when you only need to trace the lines of code that are likely to have bugs. In cases of this kind, you can use breakpoints.

A breakpoint is a stop sign placed on a line of source code that tells the debugger to pause when this line is encountered. The debugger executes every line until it encounters a breakpoint, so you can trace the part of the program at the breakpoint. Using the breakpoint, you can quickly move over the sections you know work correctly and concentrate on the sections causing problems.

There are several ways to set a breakpoint on a line. One way is to click on the line on which you want to put a breakpoint and then choose Run, Toggle Line Breakpoint from the main menu. You will see the line highlighted, and a breakpoint mark on the left of the line:

To remove a breakpoint, you may right-click on the breakpoint mark on the left of the line and select Toggle Breakpoint from the pop up menu.

As you debug your program, you can set as many breakpoints as you want, and can remove breakpoints at any time during debugging. The project retains the breakpoints you have set when you exit the project. The breakpoints are restored when you reopen it.

8.2 Starting the Debugger

There are several ways to start the debugger. A simple way is shown below:

  1. Set a breakpoint at the first statement in the main method in the Source Editor.
  2. Click on ShowCurrentTime.java . Choose Run, Debug As, Java Application from the main menu to start debugging. You will first see the Confirm Perspective Switch dialog, as shown below:

    Click Yes to switch to the Debug perspective.
The user interface for Debug perspective is shown below:

8.3 Controlling Program Execution

The program pauses at the first line in the main method. This line, called the current execution point, is highlighted in green. The current execution point marks the nest line of source code to be executed by the debugger.

When the program pauses at the execution point, you can issue debugging commands to control the execution of the program. You can also inspect or modify the values of variables in the program.

When Eclipse is in the debugging mode, the following toolbar buttons for debugging are displayed in the Debug window, as shown below. The toolbar button commands also appear in the Run menu:

The following are the commands for controlling program execution: 8.4 Examining and Modifying Variables

Among the most powerful features of an integrated debugger is its capability to examine the values of variables, array items, and objects, or the values of the parameters passed in a method call. You can also modify a variable value if you want to try a new value to continue debugging without restarting the program.

To demonstrate it, choose Run, Step Over to execute one line in the source code, and you will see the value for totalMilliseconds in the Variables pane, as shown below:

To change the value in totalMilliseconds, right-click on totalMilliseconds and select Change Value from the pop up menu. The Change Primitive Value dialog box will appear, as shown below. You can now set a new value for totalMilliseconds.

Change the value for totalMilliseconds to 1234567890123. Then, choose Run, Resume to resume execution of the program. What time is displayed in the Output window?

The debugger is an indispensable, powerful tool that boosts your productivity. It may take you some time to become familiar with it, but the effort will pay off in the long run.

After finishing debugging, you may switch to the Java perspective by choosing Window, Open Perspective, Java from the main menu.

Step 9: Creating and Testing Java Applets

You can create a Java applet in the same way you create a Java application. For example, you can create the WelcomeApplet class, as shown below:

To run an applet, choose Run, Run As, Java Applet from the main menu. Eclipse automatically creates an HTML file to contain the applet and invokes the appletviewer utility to run the applet, as shown below.

Many times we need to turn on assertions during debugging a program. E.g.,
public static int maxOf2(int a, int b) throws AssertionError
{
	int answer = 1;
	if (a>b) answer = a;
	else answer =b;
		
	assert((answer==a)||(answer==b)): "maxOf2 answer is not equal to one of the arguments";
		
	return answer;
}
To turn on the assertions: right click the class that implements this method, Then, click "Run as/Run Configurations". You will see the following window Click the "Arguments" tab and add "-ea" to the VM arguments box.