17 Introducing Eclipse

Eclipse is a modern, extensible, open source Integrated Development Environment (IDE). This means it is the latest technology for developers, can be customized by and for developers, and best of all, it is free for you to use. Some surveys (for example, at www.sdtimes.com/content/article.aspx?ArticleID=30020) suggest that more than 60 percent of Java developers use Eclipse as their primary development tool.

Eclipse comes with many important and powerful features, features that you would expect in a modern IDE. These include a file and project manager, a syntax sensitive editor, support for code refactoring, interfaces to source control systems, such as CVS and Subversion, and many others. Eclipse also provides an interface for plug-ins, which are extensions to the product that anyone can build. Many plug-ins are available, some for free and others for a fee.

The Eclipse development environment is not just for Java. It is an IDE that can be used for any programming language. There is even a plug-in that supports development for COBOL projects.

INSTALLING ECLIPSE

Image

You can get Eclipse from either the companion CD-ROM (Eclipse/eclipse-java-europa-winter-win32.zip for the Windows version) or directly from the Eclipse Web site at www.eclipse.org/downloads/download.php?file=/technology/epp/down-loads/release/europa/winter/eclipse-java-europa-winter-win32.zip. For developers using Windows, Eclipse is delivered as a zip file, which is a compressed bundle of files. You will need to unpack the contents of that bundle into a directory on your computer in order to install Eclipse, such as C:java.

If you are not sure how to unpack a zip file, here are some suggestions:

  • Double-click on the Eclipse zip file.

  • Your computer should open a new window that displays the list of files in the bundle.

  • Next, select the option to Extract all the files.

  • Choose any place on your computer to copy the Eclipse files.

  • If your computer does not know how to open a zip file, you will need to install a copy of a program that can open zip files. Try using either Winzip (www.winzip.com/downwz.htm), PowerArchiver from www.powerarchiver.com/download, or 7zip from www.7-zip.org. After installing one of these tools, your computer will be able to work with zip files. Double-click on the Eclipse zip file, and proceed as previously described.

START USING ECLIPSE

At the end of the install process, Eclipse will start up automatically. You can also start Eclipse by double-clicking on the file named Eclipse.exe in the directory where you’ve unzipped the files (for example, C:javaEclipseEclipse.exe). After Eclipse starts, you should see the introductory screen shown in Figure 17.1.

Image

FIGURE 17.1
Workspace Launcher.

Enter the location on your computer to store your Eclipse project. Try using C:java4cobolworkspace. Next, you should see the Eclipse home page, as shown in Figure 17.2.

Image

FIGURE 17.2
Eclipse home page.

The icons in the center take you to various built-in information available in Eclipse. You should spend some time reviewing the Tutorials section to get more information on how to use Eclipse and look at the code samples to get insights into how to write high-quality Java code.

To start using Eclipse, select the Workbench icon on the right. You will see the default Workbench view for Eclipse, as shown in Figure 17.3.

Image

FIGURE 17.3
Eclipse Workbench view.

You will spend most of your time in the Workbench view. You can edit, compile, and run your Java code without switching to any other window or view.

MAKE A NEW ECLIPSE PROJECT

Let’s create a new project. Select the File menu path on the top-left corner, and then select the New > Java Project menu path to get to the dialog shown in Figure 17.4.

Image

FIGURE 17.4
Create a new Java project.

For your first project, you will use the examples from Chapter 8. Type Chapter 8 in the Project Name field and click Finish. Eclipse will create the project files it needs.

Next, you need to add the existing Java classes to the project. Right-click on your new project in the Package Explorer window, and select Import. You can import into Eclipse from several places; you will import from the filesystem. Choose the General import filter type, and from the list that is displayed, select the FileSystem type. Then click the Next button, highlighted in Figure 17.5.

Image

FIGURE 17.5
Import Java files.

Image

In the next dialog, enter the directory name where you placed the Java files for Chapter 8. If you want to import the files directly from the companion CD-ROM, the name will be similar to E: ExercisesChapter08java4cobol, where E: should be replaced with the drive letter for your CD drive. Enter the directory name where the Java files are located, and press the Tab key.

Click on the selection box for the java4cobol directory name in the center of the screen, and then click the Filter Types button. In the next dialog, you can select what file types to import. Select the *.java option, then click OK.

Finally, make sure the target for the import step (that is, the Into Folder field) is the Chapter8src directory.

If you’ve done everything correctly, you should see the java files selected, as shown in Figure 17.6.

Image

FIGURE 17.6
Import from file system.

Click the Finish button. Eclipse will import these java files into the src directory in your project.

Click on the src subdirectory in the Package Explorer in Eclipse, and then the Default Package subdirectory. You should see the java source files from Chapter 8 in the Explorer. Try clicking on one of the Java files. You should see the Java code in the edit window in the center of the screen. Notice how Eclipse color codes the different parts of the Java code to make it easier for you to read and understand the code.

You can have Eclipse show you the line numbers of your source statements. Move your cursor to the left-most section of the source edit window. There should be a blue bar on the side of that window. Click the right mouse button, and select the Show Line Numbers option from that dialog.

Eclipse has already compiled the classes for you. Check the bin directory in your project workspace. If you’ve followed the directory name suggestions, that directory would be C:java4cobolworkspaceChapter8in. The bin directory contains the newly compiled class files.

RUN WITH ECLIPSE

Now it is time to execute your project with Eclipse. Choose the Run menu choice at the top of the Eclipse screen. Then choose the first item on that menu, which is Run. If Eclipse should ask you what type of project you have, select Java Application. Eclipse will now execute the HelloWorld class.

At the bottom of the Eclipse screen, in the console window, you should see the last part of the output from the Chapter 8 exercises. Use the scroll bar for that window to see the beginning of the output.

DEBUG WITH ECLIPSE

Next, you will use the Eclipse debugger. Double-click on the HelloWorld Java source file from the Package Explorer. It will open up in the edit window. Then click on the line that says System.out.println("Hello World!");.

Move your cursor to the left-most section of the edit window. There should be a blue bar on the side of that window. Click the right mouse button, and select the Toggle Breakpoint option. A small blue ball should appear on the line, indicating a breakpoint has been set for that line, as shown in Figure 17.7.

Choose the Run menu choice at the top of the Eclipse screen. Then choose the second item on that menu, Debug. Eclispe will ask you to confirm that you want to switch to the debug perspective. Choose Yes. Eclipse will now execute the Hello-World class in debug mode.

Eclipse has many features in debug mode. The most common feature, and the one you will use first, allows you to watch as you step through your program. At this point, Eclipse has started the HelloWorld program and is stopped at the line with the breakpoint. Try pressing the F6 button on your keyboard several times, until you see the second statement with System.out.println. Notice how Eclipse steps through the Java code. After the println statement is executed, notice how the console window at the bottom is updated with the output from println.

Eclipse lets you easily see the current value of objects. Position your mouse over the tempMsg variable. Notice how Eclipse shows you the contents of tempMsg.

The most common actions in debug mode are available in the Run menu choice at the top of the Eclipse screen. Select that menu to see the actions available. Choose the Resume option to let the program continue to completion.

Image

FIGURE 17.7
Set a breakpoint.

REFACTORING WITH ECLIPSE

Eclipse improves the productivity of developers in many ways. One important feature is the ability to reorganize, or refactor your Java code. Often during a Java development project, you will want to reorganize your class names or decide that an alternative packaging plan is an important improvement.

If you were to make these changes by hand, you would need to find, edit, and compile each Java file that was impacted by the change. If part of your changes included repackaging, then you would need to create new directories, and move files into the proper directory. Eclipse can make all of these changes automatically for you.

Let’s start by importing the exercises from Chapter 2. These exercises are relatively simple, but actually contained an interesting challenge in the package definition of the classes.

Switch back to the Java perspective, by selecting the Java perspective icon at the top right of the screen. Create a new Java project in Eclipse and call it “Chapter2.” You should see your new project listed in the Package Explorer Window.

Then, import the files from the Chapter 2 exercises, using the same steps used to import the files from the Chapter 8 exercises. In the Filter Types dialog in the Import function, add *.html and *.java to your choices so that you get the java source and the HelloWorld.html file, which you will need to run the applet. Make sure to point the target of the import to Chapter2src.

After Eclipse has imported the Java and HTML files, you will notice a red X box in the Package Explorer. Follow the red boxes to find the problem, by opening each subdirectory with red X boxes in the project, until you get to the appletHelloWorld Java file. Double-click on that file.

You should see the appletHelloWorld Java source opened in the code window, and a red X box on the first line, as shown in Figure 17.8.

Image

FIGURE 17.8
Applet in Eclipse with error.

Click on the red X box in the code window. Eclipse will offer a suggestion to fix the problem by adding the package declaration “applet” to the file. Double-click the suggestion, and Eclipse will add the line package applet; at the top of the HelloWorld Java source file. Save this change by pressing CTRL-S or by using the File > Save menu path. You should see the red X boxes disappear as Eclipse recompiles the applet.HelloWorld class.

Now you can run the HelloWorld applet. Select the Run > Run As > Java Applet menu choice. You should see the HelloWorld applet window pop up, with the “Hello applet World!” message in the dialog. Exit the window, and try changing the message to “Hello applet World in Eclipse!”

For the final exercise, you will deal with a more complicated example of refactoring, using the exercises from Chapter 3.

Image

Create a new Java project in Eclipse and call it “Chapter3.” Then, import the files from the Chapter 3 exercises on the CD-ROM. In the Filter Types dialog in the Import function, add *.html and *.java to your choices so that you get the java source and the HelloWorld.html file, which you will need to run the applet. Make sure to point the target of the import to Chapter3src.

You should see the familiar red X box in the Package Explorer for Chapter3. Before you fix that problem, you want to organize this project a little differently. You want to move the two classes in the default package to the package named java4cobol.

In the Package Explorer, open the Chapter3srcdefault package directory. You should see two Java classes listed, HelloWorld and ErrorMsg. Using the Shift key and the left mouse button, select both of those classes. Then, using the right mouse button, choose Refactor from the menu list. Finally, select the Move option from the refactor choices, as shown in Figure 17.9.

On the next dialog, titled Move, click the Create Package button. Enter java4cobol as the package name in the next dialog, and click Finish. Then click OK in the Move dialog. You may see a dialog indicating some potential matches have been found. If you do, click the Continue button.

In the Package Explorer, you can see that Eclipse has moved the two selected classes from the default package to the package named java4cobol. Open up that new package, and double-click on the ErrorMsg class. You can see the new package defintion inserted by Eclipse into the ErrorMsg java source file. Eclipse has also moved the java files source from the default package location to the java4cobol directory. You can check this by looking at the directory C:java4cobolwork-spaceChapter3srcjava4cobol on the file system.

Next, you will fix the two problems in the appletHelloWorld class. Open the source for Chapter3appletHelloWorld in the source edit window by finding that class in the Package Explorer and double-clicking on it.

Image

FIGURE 17.9
Refactor in Eclipse.

Add the package applet statement to the first line of that class, or use Eclipe to help you do that, just as you did in the previous exercise. This will fix the first problem.

To fix the second problem, you will need to import the ErrorMsg class from the java4cobol package you just created. After the two import statements, add a new one: import java4cobol.ErrorMsg;. The beginning of the Java source for appletHelloWorld should now look like this:

package applet;


import java.applet.Applet;
import java.awt.Graphics;
import java4cobol.ErrorMsg;

public class HelloWorld extends Applet {

...

Now, when you save the source file, the red X boxes should all disappear. And if you run that program using Run > Run As > Java Applet, you should see the correct text for this exercise in the applet dialog.

Eclipse is arguably one of the more important recent devlopments in the world of Java programming. In your future career as a Java developer, time spent learning how to use Eclipse effectively is time well spent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset