Step 9.5: Fixing import errors


Step 9.5: Fixing import errors

click to expand

I'm not going to go into detail about the SWT packages in this step. I'll cover them a little more closely in the following steps, but even so, this book is not meant to be an SWT tutorial, but more of a simple introduction. I'll leave the heavy details of SWT to another book.

In this step, I just want to show you a few things about handling errors in Eclipse.

Figure 9.26 shows a typical editing display, with a few errors. Unlike most cases, this particular class has only one problem: It can't find its JAR files. But before I get into that too much, I'd like to point out a few things.

click to expand
Figure 9.26: The Java editor with several errors.
  1. Note how there are three lines in error in this group . Eclipse identifies them with a white X in a red circle, in this case directly to the left of the line in error. The part of the line that is actually causing the error is underlined with a squiggly red line, just as Microsoft Word underlines misspelled words. This underline is sometimes called a scribble. Also notice that the rightmost column of the edit pane contains a number of small pink rectangles. Each rectangle corresponds to an error in the source, and each one appears based on it's the error's position in the source file. The earlier the error occurs in the source, the higher it is on the error bar.

    Note  

    You can also use these rectangles as navigation devices ”click on one to position the source containing the error.

  2. These two errors also have scribbles and corresponding rectangles in the error bar.

  3. Here's an interesting thing that happened while we weren't looking: This window changed itself from the Console view to the Tasks view. The Tasks view is commonly used to display errors from the other views. The Eclipse designers thought it important to "pop up" the Tasks view whenever an error occurs, to show you the current errors.

  4. Each line in the Tasks view represents an error in the source code. All errors are collected in this view (and in fact, you can add your own tasks into the view, as sort of a dynamic to-do list). The Resource and Folder columns tell you which resource the error pertains to, while the Location column tells you where in the resource the error occurs. These lines also act as another navigation device ”by clicking on a line, the editor will be positioned automatically to the corresponding error.

So, by either using the error bar of the editor pane or clicking on an error in the Tasks view, you can position the editor to show the error. In this case, you already have the errors visible in the editor pane, so you can simply go in and correct them.

Before we start fixing things, though'd like to do one extra cleanup step. This will just remove a bit of clutter in the Package Explorer pane.

q 9.5(a) Collapse the Hello project.

click to expand
Figure 9.27: Collapse the Hello project by left-clicking the minus sign.

Now you have the view in Figure 9.28.

click to expand
Figure 9.28: A slightly less cluttered Package Explorer.

Okay, the workbench is a little clearer and you can begin fixing errors. First, you need to know what those errors are. An easy way is to roll your mouse pointer over the red circle in the left-hand column of the editor pane.

q 9.5(b) Roll your mouse pointer over the error icon to see the popup of the full error description.

click to expand
Figure 9.29: Rolling your cursor over the error icon shows the entire error description in a popup.

In this case, the error is very straightforward: "The import org.eclipse cannot be resolved." This indicates that the compiler is unable to find the package org.eclipse in any of the JAR files in its classpath. To fix this error, you must include the appropriate JAR file.

External JAR files for a project are set in the Properties dialog. So are many other project attributes. The Properties dialog is very important for just about every level of resource in the Eclipse environment, so you when you have spare time, you may want to explore the various Properties dialogs.

Note  

Getting to the Properties for a project is the same as for any other resource: Right-click and select Properties. You can also use the File/Properties menu option from the main Eclipse menu bar, or you can select a resource and press Alt-Enter.

click to expand
Figure 9.30: Using the popup menu to access the Properties dialog for the SWT project.

The classpath is under the Java Build Path properties.

q 9.5(c) Select Java Build Path in the Properties dialog box.

click to expand
Figure 9.31: Select Java Build Path by left-clicking on it.

Almost there ... the classpath is maintained in the Libraries tab.

q 9.5(d) Select the Libraries tab.

click to expand
Figure 9.32: Select the Libraries tab by left-clicking on it.

At this point you can do a number of things. You may add JARs from within your workspace, define variables , add folders, and so on. For more information on this and any other dialog, press the F1 key for help. For this project, though, you need a JAR file that is not in your workspace ”that is, an external JAR file.

q 9.5(e) Click Add External JARs ...

click to expand
Figure 9.33: To add a JAR file that is not in your workspace, press Add External JARs ...

A standard file finder dialog will appear. First, navigate to your primary Eclipse folder. This is the folder "eclipse" within your Eclipse install directory, which you saved as $ECLIPSEINST in Step 2.1(h). Then open the subfolder plugins.

q 9.5(f) Open the folder named plugins.

click to expand
Figure 9.34: Open the plugins folder by double-clicking on it or selecting it and pressing Enter.

Next, you need to get to the SWT folder. It will have a different name depending on the platform and version, but it will start with "org.eclipse.swt," then end with the platform and version.

q 9.5(g) Open the folder named org.eclipse.swt.win32_2.1.0.

click to expand
Figure 9.35: Open the org.eclipse.swt.win32_2.1.0 folder.

q 9.5(h) Open the folder named ws.

click to expand
Figure 9.36: Open the ws folder.

q 9.5(i) Open the folder named win32.

click to expand
Figure 9.37: Open the win32 folder.

Finally, you will have reached a point where you see a JAR file named swt.jar. This is the JAR file that you want to include on your classpath.

q 9.5(j) Open the file named swt.jar.

click to expand
Figure 9.38: Open swt.jar to include it in the classpath.

You'll see that the JAR file has been added to the Libraries tab. Just select OK to continue.

q 9.5(k) Click OK.

click to expand
Figure 9.39: The new JAR file is in the Libraries tab, so click OK.

Your workbench should now look like the one in Figure 9.40. Notice that all the red X's are gone. There are a couple of yellow exclamation points instead. Actually, this icon consists of a light bulb and an exclamation point. The light bulbs mean you can click on the error to get a hint. The exclamation points indicate warnings; they are also in the Tasks view.

click to expand
Figure 9.40: The errors have been replaced with warnings.

Just as with errors, you can get the entire text for a warning to pop up by rolling the cursor over it, as shown in Figure 9.41.

click to expand
Figure 9.41: To see the text for a warning, roll your cursor over it.

In this case, the warning is that the import is never used. That's actually a fairly new warning message. As late as release 2.1M4 of Eclipse, this condition wasn't even flagged. But it's nice to know; you can remove the extraneous imports if you care to.

In any event, you have now fixed all the fatal errors. You can remove the imports to remove the warning messages if you'd like, or you can simply go on to Step 9.6.




Eclipse
Eclipse: Step by Step (Step-by-Step series)
ISBN: 1583470441
EAN: 2147483647
Year: 2003
Pages: 90
Authors: Joe Pluta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net