Friday, January 2, 2009

GETTING STARTED WITH ECLIPSE

OBTAINING AND INSTALLING ECLIPSE FOR YOUR HOME PC:

Since Eclipse is installed in the lab, obtaining it for your home PC is recommended, but not required.

I couldn't find an official listing of Eclipse's system requirements. The following specifications are cobbled together from various sites that use Eclipse:

· 256 MB RAM minimum (512 MB RAM recommended)

· 400 MB hard disk space minimum

· Pentium III/600 MHz or higher (1000 MHz recommended)

· Microsoft Windows 2000 Professional or Windows XP

If your home PC meets the system requirements and you'd like Eclipse for home usage, then read the rest of this section. Otherwise, jump to the next section.

Eclipse is an Integrated Development Environment (IDE) that allows you to enter, compile, and run programs. The programs can be written in one of many different languages. In order to make a particular language work in Eclipse, the language's compiler must be installed. The compiler installation is separate from Eclipse's installation. To install Sun's Java compiler, go to this Web site:

http://java.sun.com/j2se/1.5.0/download.jsp

On that Web site, you'll see information on Sun's J2SE SDK (Java 2 Standard Edition Software Development Kit) compiler package. Feel free to read about all the download links and download the ones you want, but the only one you'll really need is the JDK 5.0 Update 3 link. Click that link. Using the web site's prompts as a guide, you may have to follow a series of additional links in order to find the SDK download link that matches your particular operating system. If you're given the option of an offline download or an online download, either one will work, but I think the online one is less confusing. Download the SDK. After the download completes, install the SDK as directed by the web site's documentation.

Important:

· If your computer already has an older version of Java installed on it, that's not good enough. Many programs use code that requires JDK 5.0. So install JDK 5.0.

· You must install the Java compiler before installing Eclipse. By installing in that order, the compiler commands will be automatically added to the Eclipse IDE.

To obtain Eclipse, go to this Web site:

http://www.eclipse.org/downloads

Click on the Eclipse SDK 3.1 link (for Windows) and follow the instructions. Be aware that the download is rather large, so if you use a dial-up modem, it may take several hours.

After the download is done, use Windows Explorer to find the downloaded file. Double click the file and follow the installation instructions.

After the download is done, use Windows Explorer to find the downloaded file. Since the downloaded file is in zip format, you'll need WinZip to unzip the files. If you don't know about WinZip, go to www.winzip.com.

After WinZip is installed, double click the Eclipse download file to unzip it. When WinZip prompts you for the location of the extracted files, select your hard disk's root folder (e.g., c:\). WinZip will then automatically create an eclipse folder in your root folder and store Eclipse's files and subfolders in the eclipse folder.

Aside:

Eclipse has a hard time with spaces in folder names. That's why you should refrain from installing Eclipse in the traditional installation folder, Program Files.

After unzipping Eclipse's files, open a Windows Explorer window and look for the eclipse.exe file in your c:\eclipse folder. To start Eclipse, you'll need to use the eclipse.exe file in conjunction with the Java SDK's javaw file. More specifically, you'll need to execute this command:

C:\eclipse\eclipse.exe -vm "c:\Program Files\Java\jdk1.5.0_03\bin\javaw" -vmargs -Xmx256M

Explanation:

· The –vm " c:\Program Files\Java\jdk1.5.0_03\bin\javaw" option specifies the virtual machine (javaw is the virtual machine file). The quotes are necessary because of the space in the Program Files folder name.

· The –vmargs –Xmx256M option increases Eclipse's memory allocation (which improves Eclipse's speed).

It's legal to execute the above command from a command prompt, but that's rather cumbersome. The better solution is to add the command to your desktop and to your Start / Programs menu. Here's how to do that….

In Windows Explorer, right click on the eclipse.exe file and select Create Shortcut from the pop-up menu. Drag the newly created shortcut file to your desktop. Right click on your newly created desktop icon. Select Properties from the pop-up menu. In the properties window, click on the General tab. In the top box, overlay the given name with Eclipse. Click on the Shortcut tab. In the Target box, enter this:

c:\eclipse\eclipse.exe -vm "c:\Program Files\Java\jdk1.5.0_03\bin\javaw" -vmargs -Xmx256M

Click OK to close the properties window.

Right click on your Eclipse desktop icon and select Copy from the pop-up menu. In Windows Explorer, find the C:\Documents and Settings\All Users\Start Menu\Programs folder. Right click on that folder and select Paste from the pop-up menu.

SAVING FILES:

· Modern integrated development environments (IDEs) have trouble compiling files on diskettes. That's because modern IDE's typically require lots of extra auxiliary memory space to do their work. Thus, do not attempt to compile your programs while they are on a diskette. If you're on your home PC, I assume that you'll want to use your PC's hard disk. If you're at the school's lab, do not use the hard disks! Instead, use a USB flash drive storage device.

· If you want to buy a flash drive and use it on your home PC, make sure that your PC has a USB port. Most PC's (including our lab PC's) do have USB ports. If you can't find your computer's USB port, ask someone for help.

· To buy a flash drive, go to a computer store, Wal-mart, or shop online. This web site has decent prices:

http://shop.store.yahoo.com/digi4me/newusbflasdi.html

· Typically, to install your USB flash drive, you simply have to plug your flash drive cable into your USB port. To copy to and from your USB flash drive, open Windows Explorer and drag files with your mouse. Typically, to remove your USB flash drive, you simply have to click an unplug icon in your system tray and then unplug your flash drive from your USB port. See your USB flash drive's product packaging for details.


ECLIPSE TUTORIAL:

When working on homework assignments, use Eclipse to enter, debug, and run your programs. If you forget how to do something in Eclipse, it is up to you to carefully reread this Eclipse tutorial and figure out how to do things. To reduce the number of rereads, go through this tutorial slowly and try to remember what you're doing.

This tutorial uses x: to refer to the drive that you're saving your work on. In the lab, x: should be replaced by the USB drive (assuming that you're using a USB flash drive storage device). At home, x: should be replaced by the USB drive or the hard drive, whichever you prefer.

Whenever you are asked to do an action (left column below) that you've already done before, I will refrain from providing the supplemental information (right column below). If you don't remember the details of how to do something, look for it earlier in the tutorial. I expect you to have to look up a lot of previously covered material. This is my attempt to force you to start memorizing how to do things.

Actions

Supplemental Information

Start Eclipse.

Click on the Start menu.

Select Programs.

Select Eclipse.

Create a workspace.

Selecting Eclipse (above) should cause a Workspace Launcher dialog to appear.

In the Workspace box, enter x:\219pgms.

Since this is the first time you've specified x:\219pgms in the workspace box, you'll be creating a new workspace – the 219pgms workspace. The 219pgms workspace is contained in the 219pgms folder. If the 219pgms folder doesn't exist, Eclipse will create it for you automatically.

Make sure the "Use this as a default and do not ask again" box is unchecked.

Click OK.

Clicking OK should cause Eclipse to create the 219pgms workspace and load a Welcome screen.

Go to the workbench.

It's not required, but feel free to explore the Welcome screen's links.

The workbench is the area where you'll do all your work – where you'll enter, debug, and run code. To close the Welcome screen and go to the workbench, click on the curved arrow in the top-right corner.

If there was no prompt for workspace.

If you ever load Eclipse and the Workspace Launcher dialog doesn't appear, that means someone checked the "Use this as a default and do not ask again" box. That's a bad thing (particularly in the lab) because then all users are taken to the same workspace location and that location might not be appropriate for everyone. To correct this problem, perform these steps after getting to the Eclipse workbench:

Click the Window menu.

Select Preferences.

That should cause a Preferences window to appear.

In the container tree at the left of the Preferences window, expand the General container by pressing its associated +.

Select the Startup and Shutdown option.

Make sure the "Prompt for workspace on startup" box is checked.

Click OK.

Coding-style preferences - background information.

You'll now set Eclipse's coding-style preferences so they match the ICS Department's coding-style conventions.

If you're in the lab, you should import the ICS Department's preferences file. If you're at your home PC, you should set your preferences manually. We’ll discuss both scenarios below.…

How to set coding-style preferences when you're in the lab.

If you're at your home PC, read this section, but don't perform the operations. If you're in the lab, perform these operations:

As described above, open the Preferences window.

In the container tree at the left of the Preferences window, expand the Java container and then the Code Style container.

Select the Formatter option.

Click the Import… button in the top-right corner of the Preferences window.

In the Import Profile window, search for and select the icsDeptProfile.xml file in the x:\eclipse folder.

Click Open.

Click Finish.

Click OK.

How to set coding-style preferences and create a coding-style profile when you're at your home PC.

If you're in the lab, read this section, but don't perform the operations. If you're on your home PC, perform these operations:

Open the Preferences window.

In the container tree at the left of the Preferences window, expand the Java container and then the Code Style container.

Select the Formatter option.

In the Select a profile box, make sure that Java Conventions is selected.

Click the Show… button.

That should cause a Show Profile window to appear.

In the Show Profile window, click the Indentation tab.

In the Tab policy box, select Spaces only.

In the Indentation size box, enter 2.

In the Tab size box, enter 2.

Make sure that the Align fields in columns box is unchecked.

Make sure all the other boxes are checked.

Click the Braces tab at the top of the Show Profile window.

For all the boxes except the array initializer box, make sure that next line is selected.

For the array initializer box, make sure that same line is selected.

The rest of the code-style profile closely matches the ICS Department's coding-style conventions, so there's no need for any more preference adjustments.

Click OK.

When prompted for a profile name, enter ICS Department

How to export coding-style preferences when you're at your home PC.

I recommend that you put all of your homework into the same workspace. That way you can share your workspace's coding-style preferences. Nonetheless, there will probably come a time when you'll want to create a new workspace. When you do so, I recommend that you export your original workspace's coding-style preferences to an external file and then import that file into your new workspace. Here's how to export:

Open the Preferences window.

In the container tree at the left of the Preferences window, expand the Java container and then the Code Style container.

Select the Formatter option.

Click the Export… button in the top-right corner of the Preferences window.

That should cause an Export Profile window to appear.

In the File name box, enter x:\eclipse\icsDeptProfile and click Save. Note that a .xml extension is appended to the filename by default.

Close the Preferences window.

If you'd like to test that your export worked, shut down Eclipse, open up a new workspace, and attempt to import the icsDeptProfile.xml file.

Disable serialVersionUID warning.

This step disables an annoying compiler warning that's generated for GUI programs. GUI programs typically use the Component class and the Component class implements the Serializable interface. The Serializable interface encourages programmers to declare a serialVersionUID variable so that serialization can work properly. Serialization is an advanced topic that I'd prefer to skip for now. There's no need for you to be bothered with the annoying serialVersionUID warning. So disable the warning…

Open the Preferences window.

In the container tree at the left of the Preferences window, expand the Java container and then expand its Compiler container.

Select the Errors/Wannigs option.

In the Errors/Warnings frame, expand the Potential programming problems section.

In the Serializable class without serialVersionUID box, select Ignore.

Close the Preferences window.

Disabling the serialVersionUID warning only applies to your current workspace. If you use a different workspace, you'll need to perform this step again. This disabling step is not part of your preferences profile (and it therefore cannot be exported and imported).

Set compiler compliance level.

This step also only applies to your current workspace, so if you use a different workspace, you'll need to perform this step again.

In checking for syntax errors, Eclipse uses the JDK 1.4 compiler by default. Thus, if you use Java 5.0 code (such as a System.out.printf statement), it will lead to a compile error. To change Eclipse's compiler to JDK 5.0, do the following:

Open the Preferences window.

In the container tree at the left of the Preferences window, expand the Java container and then select the Compiler container.

In the Compiler compliance level box, select 5.0.

Close the Preferences window.

What is a project?

A project is an entity that helps to organize all the files in a particular program. A project's settings are stored in a .project file. The .project file is contained in the project's folder.

How to organize your projects.

I recommend that you use one workspace to store all your CS 219 projects. In this tutorial, you've already created a 219pgms workspace and you will create a tutorial project within that workspace. The 219pgms workspace uses a 219pgms folder. The tutorial project will use a tutorial folder which is a subfolder of 219pgms. For your homework assignments, I recommend that you create hw1, hw2, etc. projects within the 219pgms workspace.

Create a project.

Click the File menu.

Select New.

Select Project… from the pop-up sub-menu.

That should cause a New Project window to appear.

In the New Project window, select the Java Project option.

Click on the Next button.

In the Project name box, enter tutorial.

Click the Next button.

Click the Finish button.

Verify project creation.

Go to Windows Explorer and locate the x:\219pgms\tutorial folder. Verify the existence of the .project file within the tutorial folder. If you don't see the tutorial folder, try selecting Refresh from the View menu.

Create a source-code file.

In the Package Explorer frame on the left side of the workbench, right click on the tutorial project.

Select New from the pop-up menu.

Select File from the pop-up sub-menu.

That should cause a New File window to appear.

In the New File window, select the tutorial folder.

In the File name box, enter Hello.java.

Click the Finish button.

That should cause an empty Hello.java frame to appear in the workbench.

Enter this text:

Enter this text in the Hello.java frame such that is replaced with your actual name.

/****************************************

* Hello.java

*

*

* This program says "hello" to the world.

****************************************/

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world!");

} // end main

} // end class Hello

Understand the code.

The top section is known as the prologue section. It provides documentation for human readers. The compiler ignores everything between the opening /* and the closing */.

main methods should be enclosed in a class that is public and whose name matches the name of the enclosing file. For example, the main method above is enclosed in a public class named Hello. The class must be named Hello since the enclosing file is named Hello.java.

Save the source file.

Click on the save icon (the save icon looks like a diskette).

Compile your program.

Click the Project menu.

If the Build Automatically option is checked, click it (so that it becomes unchecked). Click the Project menu.

Select the Build All option or the Build Project option.

If necessary, fix compile errors.

Find your program in the Package Explorer pane.

At the left of your Eclipse window, you should see a "Package Explorer" pane that contains your workspace's project folders. If the package explorer pane is not shown, open it as follows:

Click the Windows menu.

Select the Show View option.

Select the Package Explorer option.

In the Package Explorer pane, look for the tutorial container and expand it by pressing its associated +.

In the tutorial container, look for the (default package) container and expand it.

Look for your Hello.java file inside the default package container.

Run the program.

Right click on the Hello.java file.

That should cause a pop-up menu to appear, and within that menu, select Run As.

Select Java Application in the pop-up submenu.

That should cause a progress-bar dialog box to appear (if you've got a fast computer, you might not be able to see it). The progress bar indicates that a main method is being searched for. The default place to find the main method is in the workbench's current class (i.e., the class that's currently displayed). For the current case, the main method is found in the Hello class.

After the progress-bar dialog box disappears, the Console tab should appear in the bottom of the workbench window.

Click on the Console tab.

That should cause Hello world! to appear in the Console frame.

If necessary, fix runtime errors.

Create a second program.

To create a new program, you can of course enter it from scratch as you did for the Hello.java program. As a shortcut, this time we'll copy from an existing program and edit the copy.

In the Package Explorer frame on the left side of the workbench, right click on the Hello.java file.

In the pop-up menu, select Copy.

Right click on the tutorial project.

In the pop-up menu, select Paste.

That should cause a Name Conflict dialog to appear.

In the new name box, enter Countdown (no need to enter the .java extension).

Click OK.

That should cause a Countdown.java file to appear in the tutorial project.

Open the Countdown.java file.

Double click on the Countdown.java file.

Edit the Countdown.java file.

Edit the Countdown.java file so that it contains this:

/****************************************************

* Countdown.java

*

*

* This program prints a countdown from a user-entered

* starting position.

****************************************************/

import java.util.Scanner;

public class Countdown

{

public static void main(String[] args)

{

int startPos; // starting position for countdown

Scanner stdIn = new Scanner(System.in);

System.out.print("Enter countdown starting position: ");

startPos = stdIn.nextInt();

for (int i=startPos; i>0;)

{

System.out.println(i + "...");

}

System.out.println("Lift off!");

} // end main

} // end class Countdown

Save the Countdown.java source file.

Compile your program.

Note that the build commands compile more than just the one file that's displayed in the source-code frame. The Build All command compiles all the files in your workspace and the Build Project command compiles all the files within your project. At this point, there's only one project in your workspace, so both build commands compile the same files - Hello.java and Countdown.java.

If necessary, fix compile errors.

Run the program.

Click the Run menu.

Select Run As.

Select Java Application in the pop-up submenu.

That should cause the computer to look for a main method. This time, since the workbench's current class is the Countdown class, the computer finds and uses the Countdown class's main method.

Open the Console frame.

You should see a prompt to enter the countdown starting position. Before entering your input value, you'll need to move the cursor to the right of the prompt within the Console frame. Since the prompt string includes a space after the colon, click at least one space to the right of the colon. Enter 30.

After pressing enter, your Console frame should be filled with multiple lines of 30...

Why is your Console frame filled with multiple lines of 30... ?

Terminate the program.

Your program is repeatedly printing the line 30...

That's an example of an infinite loop.

Note the red button at the upper-right corner of your Console frame. The red button allows the user to terminate a program that's currently running. (Makes sense, right? Red for stop.)

To stop the infinite loop, click the red button.

As the program terminates, the red button should become non-red.

Find the error.

An infinite loop is an example of a run-time error.

Run-time errors are often more difficult to debug than compile errors because you do not have the benefit of a compiler-generated error message.

Can you figure out the error?

Do not continue until you attempt to find the error.

Fix the error and recompile.

That's right, the for loop heading is missing its third component.

Replace the for loop heading with this line:

for (i=startPos; i>0; i--)

Recompile.

Run the program using the Run icon.

When you ran your program above, you had to wait for the computer to find the main method. After the computer finds the main method the first time, if you want to run the same main method, you can do so using this shortcut….

Look at the top of the workbench for a green-circle icon with a white triangular arrow inside it.

Hover your mouse over the icon.

That should cause the message Run Countdown to pop up.

Click the icon in order to run your Countdown program.

You should be prompted to enter the countdown starting position.

Enter 30.

Your Console frame should display this:

Enter countdown starting position: 30

30 ...

29 ...

<28>

1 ...

Lift off!

Note that you'll probably need to scroll up and down to see the entire output.

If necessary, fix errors.

Print the output.

Use your mouse to select the output (in the Console frame).

Press Ctrl+c to copy the selected text.

Your output is now copied to your computer's buffer. You'll next need to paste it into a text editor so you can print it from there. Use the NotePad text editor, not Word or WordPad. NotePad is the preferred text editor because NotePad uses monospace font. Monospace font is good because that's what your actual program output uses.

To open NotePad, click on the Start button. Then select Programs / Accessories / NotePad. That should cause a NotePad window to appear.

To paste the previously copied text into NotePad, press Ctrl+v. To print the contents of NotePad, click on the File menu and select Print …

Exit from Eclipse.

Click the X in the top-right corner or select Exit from the File menu.

Start Eclipse

This time when you start Eclipse, the Workspace Launcher dialog should display x:\219pgms as the suggested workspace.

Click OK.

That should cause Eclipse to load your 219pgms workspace and your tutorial project.

If the workspace launcher doesn't work.

Eclipse is open source and fairly new. Thus, there are bugs. I've found that I'm occasionally unable to reload a workspace. If that happens, use Windows Explorer to delete the workspace's .metadata folder. Then (re)create your workspace as described above.

Exit from Eclipse.

0 comments:

Search

My Blog List