Eclipse Tutorial

Eclipse is a full-features Integrated Development Environment. It is installed on CS Linux machines. If you would like to download and install it on your own machine you may get it from the main Eclipse page at http://www.eclipse.org/.

        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 &".

 

Note: This tutorial was originally written for Eclipse 3.1 and then modified to match the newer version of Eclipse that is installed now. Therefore, some of the screenshots may look differently.

Eclipse uses a workspace to store your configuration settings and all of the files you are currently working on. The default workspace is called “workspace” and a folder with such name is created automatically. It is possible to create more than one workspace but one will suffice for this course. You can always create a new workspace or switch a workspace by choosing File, Switch Workspace from the main menu.

        Step 2: Choosing a Perspective

Eclipse's main window is divided into smaller windows called views.  Views display different information about your work. To open a specific view, click Window, Show View, and select the desired View.

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. The Java perspective is a default perspective. You may switch perspectives by choosing Window, Open Perspective from the main menu, as shown below.

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve3.jpg

Close the Welcome window and you will see the Eclipse user interface. The current perspective is displayed on the title bar.

 

 

Step 3: Creating a New Java Project

To create a project, choose File, New, Java Project to display Create a Java Project wizard, as shown below.

Type myprograms in the Project name field. Make sure that you selected the option "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 Java Class wizard as shown below. Type Welcome in the Name field. Check the option public static void main(String[] args). This will automatically create main  method stub in your program.

 

 

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:

 

Other indicators of errors are red circle with a cross to the left of the line and red rectangle to the right of the line (see above). Clicking on them gives you description of the errors, e.g. “Syntax error, insert ‘;’ to complete BlockStatements”.

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.

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve11.jpg

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

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve12.jpg

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

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve13.jpg

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:
3.   
4.  % java Welcome
5.  Welcome to Java
  1. You may also compile the program using the javac command at the prompt:
7.   
8.  % 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 a file system (select General, File System).

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve14.jpg

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. Type /home/faculty5/ipivkina/cs272 in the From directory field. Click on the left panel to have the directory appear on the left panel as shown below. (Note: since the file is in my personal directory you will not be able to find it using Browse button; however, if you type the directory name you should be able to access it). 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:

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve15.jpg

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.

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve16.jpg

 

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 the directory /home/faculty5/ipivkina/cs272/
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:

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve17.jpg

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:

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve18.jpg

Click Yes to switch to the Debug perspective.

The user interface for Debug perspective is shown below:

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve19.jpg

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:

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve20.jpg

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:

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve21.jpg

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.

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve22.jpg

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.

Description: \\Srvpc.cs.nmsu.edu\ipivkina\public_html\cs272\eclipsetutorial_files\new_ve24.jpg