DOC

Chapter 2[1]. Hello Eclipse

By Keith Reed,2014-03-29 16:16
7 views 0
Chapter 2[1]. Hello Eclipse

    Chapter 2. Hello Eclipse

    Course points

    ; Install and start the Eclipse workbench.

    ; Create a new Java project.

    ; Use the Java class wizard to create several classes.

    ; Run your application within the workbench.

    Reading

    2.1 Installation and Startup

    If you have not already done so, download the Eclipse Software Development Kit(SDK) from http://www.eclipse.org. ;eclipse-SDK-3.2.1-win32.zip

    You must have installed Java JDK 1.4.1 or later before starting Eclipse. JDK downloads are available from java.sun.com/j2se/. If you are unsure which JDK version you are running,

    open a command window and type "java -version." jdk-1_5_0_02-windows-i586-p.exe

    Eclipse is not distributed with an automated installer that creates a desktop icon and program startup group. Instead, just unpack the ZIP archive. This will create an eclipse

     for Windows computers. directory at your selected location, typically at C:\eclipse

    Within this folder, you will find an executable file named eclipse.exe on Windows. You can browse to this location and run the program here, but it's most convenient to create a desktop shortcut icon for starting Eclipse. Running this program should display the dialog shown in Figure 2-1.

    Figure 2-1. Select the current workspace when starting Eclipse.

    You should increase the amount of heap memory allocated to Eclipse. This is not required, but it will speed up execution when developing and debugging large applications. To allocate 256MB, change your startup command (or target in your desktop icon) to read as follows: C:\eclipse\eclipse.exe vmargs Xmx256M

    You might have several versions of Java installed on your computer and need to specify which one is used to run Eclipse. If this is the case, add the vm option on the Eclipse startup program and provide the JVM command path. Also add the -showlocation option to include the current workspace path on the workbench title. Your startup would now appear similar to this:

    C:\eclipse\eclipse.exe -showlocation

     -vm c:\j2sdk1.4.2_02\bin\javaw.exe -vmargs Xmx256M

2.2. Eclipse IDE Workbench

    When you start Eclipse, you are greeted with a Welcome page that provides links to several tutorials and examples.

We'll bypass the introductory tour and go straight to development, so click the arrow icon

    in the upper-right corner of the Welcome screen, which will take you to the workbench as shown in Figure 2-2. This is called the Resource perspective.

    Tip: You can get back to the Welcome tour at any time by selecting the menu Help > Welcome.

    toolbar Perspective switcher

    Editor area

    view

    Figure 2-2. The Eclipse workbench resource perspective.

    An Eclipse perspective defines a selection of views and their layout within the workbench window. Several perspectives are available, where each is configured for a particular task, such as Java development, debugging, or browsing CVS repositories. This initial resource

    perspective is the most generic and may be used to view and edit files in any kind of project.

2.3. Create a New Java Project

    We are now ready to start the first iteration of our product catalog application. Our first goal is to get something running and learn how to work in Eclipse, so we'll start very simply. A class diagram for the first project is shown in Figure 2-3.

    Figure 2-3. First iteration design for order processing application.

The following tasks are completed in this section:

    ; Switch to the Java perspective.

    ; Create a Java project.

    ; Customize code templates used by the Java class wizard.

    ; Create two Java classes that implement Figure 2-3.

    ; Create a class with a main() function to test our design. 2.3.1. Open the Java Perspective

    We'll move through these first steps in quick succession. Most of your Java development

    work will be done in the Java perspective 视图. Get there by selecting the menu Window > Open Perspective > Java. You will now see both the Java and Resource icons appear in the perspective switcher located in the upper-right corner of the workbench window. Depending

    on your screen resolution分辨率, one or more icons may be hidden, and you'll see a >> symbol on the right. If so, click on the left edge of the perspective switcher and drag it left

    until all perspective icons are showing.

    Look for three wizard icons in the center of the toolbar, as shown in Figure 2-4.

    Figure 2-4. Three wizards for Java projects.

    Wizards are used extensively经常 in the Eclipse workbench to give guidance when performing multi-step activities.

    These three wizards perform the following tasks:

    ; Create a New Java Project.

    ; Create a New Java Package.

    ; Create a New Java Class.

    2.3.2. Create a Java Project

    We'll follow a common convention规约and use qualified限定 names for projects in the Eclipse workspace that reflect the primary 主要Java package contained in each project. Our product catalog 目录project is thus named com.eclipsedistilled.catalog.

    ; Click the New Java Project wizard to create a project that will contain Java code.

    ; Enter the project name: com.eclipsedistilled.catalog

    ; Check 确定option: Create separate source and output folders.

    ; Click Finish to create the project.

    Figure 2-5. New Java Project wizard.

    The project wizard is shown in Figure 2-5. Be sure to check the layout option to Create

    separate source and output folders. This option creates an src folder to contain all source code and a separate bin folder to contain the compiled class files. Without this option, .java and .class files are co-mingled 混合in the same folders.

    2.3.3. Create a Java Package

    ; Click the New Java Package wizard to create a new Java package.

    ; Enter the package name: com.eclipsedistilled.catalog.

    Figure 2-6. Java Package Explorer view with one empty package.

    2.3.4. Create a Java Class

    ; Select the com.eclipsedistilled.catalog package within the src folder in the Package

    Explorer view. A Java package is indicated标志 by an icon resembling 象征a box

    wrapped 捆绑with string.

    ; Click on the New Java Class wizard to create a new Java class.

    ; Enter the class name: Catalog.

    ; Click Finish to create the class.

    The New Java Class wizard is shown in Figure 2-7. Other options are available for creating inner classes, specifying声明 a superclass, implementing interfaces界面, and generating

    method stubs存根.

    Figure 2-7. The New Java Class wizard.

    Your Java perspective Java视图will now look similar to Figure 2-8. The Java file editor

    contains a new class that is created from a template and filled in with your selections from the wizard. Like most aspects of Eclipse, you can customize this template to modify the content of default comments注释.

    Figure 2-8. Java perspective with one project containing one class.

    2.3.5. Customize Code Templates

    Eclipse contains many code generation templates代码生成模板 that you can customize,

    but two of these should be modified right at the beginning because they affect every new file. In Figure 2-8, you can see it.

    Figure 2-9. Code templates preferences.

    ; Select the menu Window > Preferences, and then in the resulting dialog select Java >

    Code Style > Code Templates.

    ; Select Code > New Java files, as shown in Figure 2-9.

    ; Click the Edit… button, which opens the dialog shown in Figure 2-10. Modify the

    comment block, but at least for now, do not change the template parameters 参数

    following the comment. Click OK.

    ; Select Comments > Types.

    ; Click the Edit… button to modify the class declaration comment. You might want to

    include standard Javadoc tags such as @author and @version. Click OK. ; Click OK to save the new preferences.

    Figure 2-10. Edit the template for new Java files. Our initial implementation also requires a second class:

    ; Select the com.eclipsedistilled.catalog package in the Package Explorer view.

    ; Click on the New Java Class wizard to create a new Java class.

    ; Enter the class name: CatalogItem.

    ; Click Finish.

    2.3.6. Writing Methods

    We'll keep these classes very simple for now 目前and focus on getting this program running. Open the editor for Catalog.java and edit it as follows to add an items field and

    getItems() method.

    com.eclipsedistilled.catalog/Catalog.java

    package com.eclipsedistilled.catalog;

    import java.util.List;

    import java.util.Vector;

public class Catalog {

     private List items = new Vector();

     public List getItems() {

     return items;

     }

    }

Open the CatalogItem.java file and add a name field with associated getName() and

    setName() methods.

    com.eclipsedistilled.catalog/CatalogItem.java package com.eclipsedistilled.catalog;

public class CatalogItem {

     /** The catalog item name */

     private String name;

     public CatalogItem(String name) {

     setName(name);

     }

     public String getName() {

     return name;

     }

     public void setName(String name) {

     this.name = name;

     }

    }

    When finished making changes in an editor, press Ctrl+S, press the Save icon on the toolbar, or select File > Save from the workbench menu. You may not realize it at this point,此时 but your code is already compiled! The project is rebuilt 从新编译automatically whenever a Java source file is saved. Switch back to the Resource perspective by clicking on

    its icon in the perspective switcher. In the Navigator view shown in Figure 2-11, expand the src and bin folders where you will see a compiled .class file for each .java file.

Report this document

For any questions or suggestions please email
cust-service@docsford.com