Tuesday, March 31, 2009

How to use Eclipse

Eclipse is a Java-based, extensible open source development platform. It is simply a framework and a set of services for building a development environment from plug-in components. Fortunately, Eclipse comes with a standard set of plug-ins, including the Java Development Tools (JDT). Eclipse also includes the Plug-in Development Environment (PDE), which is mainly of interest to developers who want to extend Eclipse, since it allows them to build tools that integrate seamlessly with the Eclipse environment. Because everything in Eclipse is a plug-in, all tool developers have a level playing field for offering extensions to Eclipse and providing a consistent, unified integrated development environment for users.

Although Eclipse is written in the Java language, its use isn't limited to the Java language. For example, plug-ins are available or planned that include support for programming languages like C/C++ and COBOL. The Eclipse framework can also be used as the basis for other types of applications unrelated to software development, such as content management systems.
The Eclipse Workbench
The Eclipse Workbench consists of several panels known as views, such as the Navigator view at the top left. A collection of panels is called a perspective. The default perspective is the Resource Perspective, which is a basic, generic set of views for managing projects and viewing and editing files in a project.
The Navigator view lets you create, select, and delete projects. The panel to the right of the Navigator is the editor area. Depending on the type of document selected in the Navigator, an appropriate editor window opens here. If Eclipse does not have an appropriate editor registered for a particular document type (for example, a .doc file on a Windows® system), Eclipse will try to open the document using an external editor.
The Outline view, below the Navigator, presents an outline of the document in the editor. The precise nature of this outline depends on the editor and the type of document; for a Java source file, the outline displays any declared classes, attributes, and methods.
The Tasks view gathers information about the project you are working on. This can be information generated by Eclipse, such as compilation errors, or it can be tasks that you add manually.
Now, we'll create a project in the Navigator. Right click in the Navigator view, and then select New > Project. When the New Project dialog box appears, select Java on the left. Standard Eclipse has only one type of Java project, named Java Project. If there were a plug-in installed to supply support for JSPs and servlets, we would see an additional option here for Web applications. For now, select Java Project, enter "Hello" when prompted for the project name, then click Finish.
Next, we'll take a look at the Java perspective. Depending on how you like to manage your screen, you can either change the perspective in the current window by selecting Window > Open Perspective > Java or you can open a new window by selecting Window > New Window and selecting the new perspective.
The Java perspective, as you might expect, has a set of views that are better suited for Java development. One of these includes, as the top left view, a hierarchy containing various Java packages, classes, JARs, and miscellaneous files. This view is the called the Package Explorer. Also notice that the main menu has expanded to include two new menu items: Source and Refactor.
Creating a new class in the Java perspective
Using the Java perspective, right-click on the Hello project, and select New > Class, as shown in Figure. In the dialog box that appears, type "Hello" as the class name. Under "Which method stubs would you like to create?" check "public static void main(String[] args)," then Finish. This will create a .java file with a Hello class and an empty main() method in the editor area as shown in Figure.
The Hello class in the Java editor
Additionally, you can invoke code completion by using Ctrl+Space. Code completion provides a context-sensitive list of suggestions selectable by keyboard or mouse. The suggestions can be a list of methods specific to a particular object, or a code snippet to expand, based on various keywords like for or while.
Syntax checking depends on incremental compilation. As you save your code, it is compiled in the background and checked for syntax errors. By default, syntax errors are underlined in red, and a red dot with a white X appears in the left margin. Other errors are indicated with a light bulb in the editor's left margin; these are problems that the editor might be able to fix for you -- a feature called Quick Fix.
The code above has a light bulb next to the for statement because the declaration for i has been omitted. Double-clicking on the light bulb will bring up a list of suggested fixes. In this case, it will offer to create a class field i, a local variable i, or a method parameter i; clicking on each of these suggestions will display the code that would be generated. Figure shows the list of suggestions and the code it suggests for a local variable.
Quick Fix suggestions
Double-clicking on the suggestion inserts the code in the proper location in the code. Once the code compiles without error, you can execute the program by selecting Run from the Eclipse menu. A Launch Configurations dialog box appears, with appropriate defaults; click Run at the bottom right. A new tabbed panel appears in the lower panel (the Console), displaying the program's output as shown in Figure.
Java debugger
You can also run the program in the Java debugger. First, set a breakpoint in main() System.out.println() by double-clicking in the gray margin on the left side of the editor view, next to the call to System.out.println(). A blue dot will appear. From the Run menu, select Debug. As described above, a Launch Configurations dialog will appear. Select Run. The perspective will automatically change to the Debug perspective, with a number of interesting new views as shown in Figure.
First, notice the Debug view at the top left of the perspective. This view shows the call stack and has a toolbar in the title bar that allows you to control the execution of the program, including buttons to resume, suspend, or terminate the program, step into the next statement, step over the next statement or return from a method.
The panel at the top right contains a number of tabbed views, including Variables, Breakpoints, Expressions, and Display. I've clicked Variables so we can see the current value of i.
You can obtain more information about any of the views with the context-sensitive help: click on the title of the view and press F1.