BUY THIS BOOK
Add to Cart

Print Book $44.95


Safari Books Online

What is this?

Add to UK Cart

Print Book £31.95

What is this?

Looking to Reprint this content?


Eclipse Cookbook
Eclipse Cookbook

By Steve Holzner
Price: $44.95 USD
£31.95 GBP

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Basic Skills
If you're a Java™ programmer, Eclipse is the best thing since sliced bread. Eclipse not only takes the rough edges off Java development better than any other product, but also it's a free download!
Even if you develop with just the Java compiler, javac, Eclipse can make life easier for you. In fact, Eclipse's Integrated Development Environment (IDE) makes the development process as close to fun as it can get: the first time that Java programmers fire up Eclipse and start to use it, they often find themselves thinking, This is great!
As with any extensive programming tool, however, Eclipse has a learning curve. In this chapter, we will brush up on fundamental Eclipse skills, from installing Eclipse to creating a simple Java application. Some of these skills are covered for the sake of reference, and you already might be adept at them, while others might be new to you. The goal with this book is to push the Eclipse envelope; to do that, you must have the solid foundation this chapter provides.
You want to try out Eclipse.
Eclipse is a free download, and you can pick it up at http://www.eclipse.org. Just click the Downloads link located on the left side of that page.
The current download URL is http://www.eclipse.org/downloads/index.php. The download page appears in Figure 1-1.
Figure 1-1: The Eclipse download page
Click a mirror site close to you, choose the version of Eclipse you want, and then select your operating system (Eclipse works with everything from Linux to AIX to Windows). The download starts automatically.
You can download four different versions of Eclipse:
Release builds
These releases are for general use. In most cases, when you download Eclipse you'll use one of the release versions. These builds have been tested, and the chance of coming across serious bugs is minimal. If Eclipse were for sale, this is the version that would be selling.
Stable builds
Stable builds are much like beta versions. The Eclipse team treats this build as relatively stable, but you might run across problems, as in any beta version. If you're interested, this is where you'll find forthcoming Eclipse features.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
If you're a Java™ programmer, Eclipse is the best thing since sliced bread. Eclipse not only takes the rough edges off Java development better than any other product, but also it's a free download!
Even if you develop with just the Java compiler, javac, Eclipse can make life easier for you. In fact, Eclipse's Integrated Development Environment (IDE) makes the development process as close to fun as it can get: the first time that Java programmers fire up Eclipse and start to use it, they often find themselves thinking, This is great!
As with any extensive programming tool, however, Eclipse has a learning curve. In this chapter, we will brush up on fundamental Eclipse skills, from installing Eclipse to creating a simple Java application. Some of these skills are covered for the sake of reference, and you already might be adept at them, while others might be new to you. The goal with this book is to push the Eclipse envelope; to do that, you must have the solid foundation this chapter provides.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Getting Eclipse
You want to try out Eclipse.
Eclipse is a free download, and you can pick it up at http://www.eclipse.org. Just click the Downloads link located on the left side of that page.
The current download URL is http://www.eclipse.org/downloads/index.php. The download page appears in Figure 1-1.
Figure 1-1: The Eclipse download page
Click a mirror site close to you, choose the version of Eclipse you want, and then select your operating system (Eclipse works with everything from Linux to AIX to Windows). The download starts automatically.
You can download four different versions of Eclipse:
Release builds
These releases are for general use. In most cases, when you download Eclipse you'll use one of the release versions. These builds have been tested, and the chance of coming across serious bugs is minimal. If Eclipse were for sale, this is the version that would be selling.
Stable builds
Stable builds are much like beta versions. The Eclipse team treats this build as relatively stable, but you might run across problems, as in any beta version. If you're interested, this is where you'll find forthcoming Eclipse features.
Integration builds
These are a step below stable builds; their components have been fairly well tested, but the way they operate together still might be problematic. If things work out OK for an integration build, it might be made into a stable version.
Nightly builds
The most experimental and risky of all publically available Eclipse builds, these are created every night by the Eclipse team, and they come with virtually no guarantee. My experience with these builds suggests that you can run into substantial problems, so beware here.
Usually, you'll pick the most recent release version of Eclipse. Allow some time for the download; depending on your operating system, most current release versions are more than 60 MB.
Chapter 1 of Eclipse (O'Reilly); the Eclipse site, http://www.eclipse.org; the technical articles at www.eclipse.org/articles/index.html; the newsgroups at http://www.eclipse.org/newsgroups/index.html
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Installing and Running Eclipse
You want to install Eclipse and get it running on your machine.
After you've downloaded it, installing Eclipse is not hard: unzip or untar the Eclipse download, and you're ready to go. Because you've downloaded the version of Eclipse targeted to your operating system, you'll find the executable file as soon as you uncompress Eclipse. To run Eclipse, just run that executable file.
One of the great things about Eclipse is the ease with which you can install it. Wherever you unzip or untar it is its home. You also can have parallel installations of Eclipse; just decompress it in the various directories you want and run the executable file, such as eclipse.exe.
Users of big, invasive IDEs will appreciate the fact that Eclipse installs quickly, does not require multiple reboots, and does not include hidden spyware. Windows developers will be relieved to learn that Eclipse does not install itself in the Windows registry, with all the attendant problems that can cause. So, (re)installation is painless.
When you first run Eclipse, you'll see the Resource perspective by default, as shown in Figure 1-2. As discussed in the Recipe 1.6 later in this chapter, a perspective presents an arrangement of windows to the user. If you open a particular perspective over and over, you'll always get the same set of windows. The Resource perspective is a general perspective that is good for resource management, particularly file handling. But we're not going to work with it here because everything we need, including virtually all functionality that the Resource perspective offers, is in the Java-oriented perspectives, particularly the Java perspective, discussed later in this chapter.
The Resource perspective doesn't have to be your default perspective. Select Window Preferences Workbench Perspectives to choose a default perspective. Java programmers often select the Java perspective here, which is the main perspective for Java development.
Figure 1-2: The Resource perspective
You must have Java installed on your machine to start Eclipse. If you start Eclipse for the first time and see a dialog box with a message beginning with the sentence "A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse," download and install Java first. You can get Java for free at
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Understanding Your Workspace
What's meant by the term workspace? And what's a plug-in?
Although you might think of Eclipse as a Java IDE, it comprises a number of components that act together behind the scenes in Eclipse's workspace. A plug-in is a software tool that accomplishes a specific task in Eclipse, such as allowing you to edit an Ant file, compile a Java file, or drag and drop GUI elements. The workspace, along with the Eclipse workbench, the team component, and the help component, are all parts of the overall Eclipse platform, and serve as the foundation for plug-ins to interact with the Eclipse core software.
The Java IDE you work with is the Java Development Toolkit, or JDT. The JDT is not an integral part of Eclipse; it's a plug-in. Eclipse is really composed of the Eclipse Platform, which provides support for other tools. These tools are implemented as plug-ins, allowing the platform itself to be a relatively small software package.
Eclipse comes with a number of plug-ins, including the JDT. Another important plug-in is the Plug-in Development Environment (PDE), which enables you to develop your own plug-ins. If you want to develop in a language other than Java, you get the corresponding plug-in for that language, and many are available.
Besides using different programming languages, you can change the human language that Eclipse uses as well. Different languages often are supported with what are called plug-in fragments. Plug-in fragments are available for numerous languages, including Japanese, Korean, German, French, Italian, Portuguese, Spanish, and even traditional and simplified Chinese.
Knowing the parts of Eclipse is essential to working with it for anything but the most casual use. If you don't have at least an idea of what parts do what, you'll end up confused when you encounter the barriers between these components, which can make Eclipse's behavior seem inconsistent. For example, when you know that the JDT is different from other plug-ins, you won't be surprised when options available in the JDT aren't available in other plug-ins.
The Eclipse Platform consists of several components: the platform kernel, the workspace, the workbench, the team component, and the help component. You can see an overview of these components in Figure 1-3. Plug-ins are loaded when Eclipse starts; the plug-ins also appear in Figure 1-3.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Running Multiple Eclipse Windows
You want to run multiple Eclipse windows at the same time, possibly including different versions of Eclipse.
Launch Eclipse more than once, and you'll get multiple Eclipse windows.
Running multiple Eclipse windows is not a problem, as shown in Figure 1-4.
Figure 1-4: Running multiple Eclipse windows
You have choices here, as explained in the following sections.

Section 1.4.3.1: Multiple Eclipse windows, same workspace

To open multiple Eclipse windows using the same workspace, select Window New Window. It's a good idea to use this technique if you want to work in two different perspectives (such as the Java and Debug perspectives) at the same time in different windows.

Section 1.4.3.2: Multiple Eclipse windows, multiple workspaces

You can launch Eclipse so that it uses the workspace of your choice instead of the default one. To do that, enter eclipse -data newWorkSpacePath -showLocation on the command line. This starts Eclipse with the workspace given by newWorkSpacePath (the -showLocation option makes windows show their location, making it easy to remember where you are if you launch multiple windows). You would use this technique if you want to partition projects into different workspaces to keep them separate.

Section 1.4.3.3: Multiple Eclipse installations

You can have multiple Eclipse installations on the same machine, including different versions. Just unzip or untar them in different directories; they won't conflict. This can be useful if you want to try out some of the new features in nonrelease builds, or if you don't want to start Eclipse from the command line to work with different workspaces.

Section 1.4.3.4: Runtime workbench

You can launch a runtime workbench by selecting Run Run As Run-time Workbench. This can be useful to test plug-ins, as we'll do in Chapter 12. The plug-in you're developing will appear in the new workbench, ready to be tested.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating a Java Project
You want to start programming some Java. Where do you start?
Select File New Project.
In Eclipse, all code—Java or otherwise—has to go into a project, and creating Eclipse projects is a basic skill. Projects organize your files, classes, libraries, and exports. Over the next few recipes, we're going to create a Java project that will use the code you see in Example 1-1 to display some simple text, "Stay cool."
Creating a Java project is a basic skill, and this chapter is all about basic skills. However, there's much more that we don't have space for here. For additional details on creating Java projects, see Chapter 3 and Chapter 4.
Example 1-1. The FirstApp.java example
public class FirstApp
{
  public static void main(String[] args)
  {
    System.out.println("Stay cool.");
  }
}
To get started, create a new Java project by selecting File New Project in Eclipse, and then open the New Project dialog, shown in Figure 1-5.
Figure 1-5: The New Project dialog
We want to create a Java project, so select Java in the left pane and Java Project in the right pane. Click Next, and in the next dialog name the project FirstApp, as shown in Figure 1-6.
Figure 1-6: Naming a project
Click Finish to finish creating the project. (If you click Next, additional options for creating projects appear in the dialog that opens, but this chapter deals with basic Eclipse skills; you can learn more about project creation options in Chapter 3.)
If you've opened Eclipse for the first time and/or the Resource perspective is the only perspective open, Eclipse will ask you if you want to switch to the Java perspective, as shown in Figure 1-7. Click Yes.
Figure 1-7: Switching perspectives
After this new project, FirstApp, is created, it's opened in the Java perspective. We've already discussed perspectives informally, but now that we're about to work with them directly we'll take a more in-depth look in the next two recipes. We'll continue developing the code for this example in the recipes that follow.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Managing Perspectives, Views, and Editors
How do you work with Eclipse views, editors, and perspectives?
To use Eclipse, you need to know what view, editor, and perspective mean:
View
This is a window that gives you a graphical display of your data, whether that data is text, bulleted lists, a GUI, images, and so on.
Editor
An editor is much like a view, except that the data in it is editable. When you're working on your code, you edit it in an editor, which is usually the central window displayed in the workbench.
Perspective
A perspective is a well-defined collection of views and editors. When you open a perspective, its views and/or editors appear in the workbench.
Views display data but don't let you edit it; editors both display data and make it editable. Because screen space is always at a premium in GUIs, views are often stacked one on top of another. You select the one you want to see using tabs that appear on the edge of the stacked views.
If you want to reopen a view you've closed by mistake, select Window Show View, and choose the view you want from the menu that appears.
When you open code or other resources, their data will appear in an editor so that you can work on it. Eclipse automatically selects the right editor for the resource you're opening, based on the resource's file extension: the JDT's Java code editor for Java code (.java files), an XML editor if you have one installed for XML files (.xml files), and so on. You even can open Microsoft Word documents (.doc files) in an editor; Eclipse displays a Microsoft Word window in the editor space using Windows Object Linking and Embedding (OLE).
You can set which Eclipse editor or external program to use to open a specific type of file based on its file extension. Just select Window Preferences Workbench File Associations, select the file type, and associate an editor or program with it (if nothing else, you can use Eclipse's default text editor). You also can run programs outside Eclipse as external tools, invoked by selecting Run
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mastering the Java Perspective
What is the Java perspective, and what can I use it for?
You can see the parts of the Java perspective, as presented by the Eclipse JDT, in Figure 1-8. The Java perspective is showing FirstApp, the project we've been developing over the previous few recipes (you can see this project in the Package Explorer on the left side of the figure).
Figure 1-8: The Java perspective
This is the main perspective you use for Java development; if you're going to be using Eclipse on a daily basis, this is the perspective you'll be staring at a lot. In other words, this is your development environment.
You also can open the Java perspective at any time by selecting Window Open Perspective Java.
Take a look at the Java perspective in Figure 1-8. At the top of this perspective are the standard workbench menu bars and toolbars, now populated with the items for the Java perspective. You're going to get familiar with those items in this and in coming chapters.
You move the toolbars in Eclipse by dragging them. If you don't want to move the toolbars inadvertently, select Window Lock the Toolbars.
On the left side of Figure 1-8, you see the Package Explorer and Hierarchy views stacked on top of each other. You can use the tabs at the bottom to flip between these two views.
The Package Explorer view is especially important for Java programming and is the view you use most frequently in Java development. It gives you an overview of your projects in Java terms, enabling you to navigate through files and classes. Double-clicking an item in the Package Explorer opens that item in the central editor window in the Java perspective.
The Hierarchy view enables you to work with type hierarchies. To use this view, right-click an item in the perspective's editor, and choose Open Type Hierarchy. This makes the item's type hierarchy appear in this view as a clickable inheritance tree. Working with an item's type hierarchy like this can be great if you want to look up the syntax of an inherited method.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating a Java Class
You want to create a new Java class in an existing Java project.
When you have the Java perspective open and have a Java project selected in the Package Explorer, you can create new classes in Eclipse in several ways. You can use the toolbar item with the circled C icon, you can select File New Class, or you can right-click a project in the Package Explorer and select New Class in the context menu. All these methods open the New Java Class dialog.
The New Java Class dialog is shown in Figure 1-9.
Figure 1-9: Creating a new Java class
Over the previous few recipes, we've been developing a short Java project, FirstApp. Now we're going to use the following code to display a message in that project:
public class FirstApp
{
  public static void main(String[] args)
  {
    System.out.println("Stay cool.");
  }
}
Creating Java classes is a fundamental skill for most Eclipse developers, so I'm going to cover the basics here. Note the options in this dialog: you can set a class's access specifier as public, private, or protected; you can make the class abstract or final; you can specify the new class's superclass (java.lang.Object is the default); and you can specify which, if any, interfaces it implements. Class creation is covered in more detail in Chapter 3.
In this book, you'll put examples into Java packages to avoid any conflict with other code; here, we use packages named after the example's chapter, such as org.cookbook.ch01. Enter the name of this new class, FirstApp, in the Name box and the name of a new package for this class, org.cookbook.ch01, in the Package box. Note in particular that under the question in this dialog "Which method stubs would you like to create?" that we're leaving the checkbox marked "public static void main(String[] args)" checked. Doing so means that Eclipse will create an empty main method automatically. Click Finish to accept the other defaults.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Completing Code Automatically
While entering code, you forgot the name of either a method you wanted to call or some of a method's parameters.
Use Eclipse's code assist (also called content assist) to help out. When you enter the name of an object or class in the JDT code editor followed by a period (.) and then pause, code assist displays the members of that object or class, and you can select the one you want. You also can bring up code assist at any time (e.g., when you've positioned the cursor inside a method's parentheses, and you want to see what arguments that method takes) by pressing Ctrl-Space or by selecting Edit Content Assist.
Code (or content) assist is one of the good things about using a full Java IDE. It's an invaluable tool that accelerates development, and it's a handy resource that you'll probably find yourself relying on in time. In the code example we've been developing over the previous few recipes, enter the following code to display some text:
public class FirstApp
{
  public static void main(String[] args)
  {
    System.out.println("Stay cool.");
  }
}
To work with code assist, enter System. in the main method of the FirstApp project, then pause. Code assist displays the classes and methods in the System namespace, as shown in Figure 1-11.
Figure 1-11: Using code assist
Double-click out in the code assist list so that code assist inserts that member into your code, insert a period so that the phrase now reads System.out., and pause again. Code assist now displays the methods of the out class. Double-click the code assist suggestion println(String arg0), and code assist inserts the following code into the main method:
public class FirstApp
{
  public static void main(String[] args)
  {
    System.out.println( )
  }
}
Edit this to add the text Stay cool.. Note that code assist adds the closing quotation mark automatically as you type:
public class FirstApp
{
  public static void main(String[] args)
  {
    System.out.println("Stay cool.")
  }
}
As soon as you enter this code, Eclipse displays it with a wavy red underline, shown in Figure 1-12, to indicate that a syntax problem exists. Rest the mouse cursor over the new code, and a tool tip appears, also shown in Figure 1-12, indicating that a semicolon is missing. Note also that a red box (displayed in stunning black and white in the figure) appears in the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Running Your Code
How do you run Java code from Eclipse?
Select Run Run As. In the list that appears, select one of the following items: Java Applet, Java Application, JUnit Test, or Run-time Workbench.
To run the code developed over the previous few recipes, select Run Run As Java Application (Eclipse will prompt you to save the file if you haven't already done so). Figure 1-13 shows the results, with the output of our code, Stay cool., appearing in the Console view at the bottom.
Before running a Java program for the first time, it's a good idea to check if Eclipse is using the JRE or JDK you want it to use. By default, Eclipse locates an installed JRE or JDK, but it might not be the one you want to use (for example, Eclipse might have located the JRE installed for a browser you're using). To check which JRE or JDK Eclipse is using, select Window Preferences to open the workbench preferences. Select Java Installed JREs in the tree pane on the left to display the Installed Java Runtime Environments preference page. The JRE or JDK Eclipse is using is checked; you can check other JREs or JDKs to make Eclipse use them instead. We'll discuss adding other JREs and JDKs in the next chapter.
Figure 1-13: Running Java code
And that's it—you've developed and run your first Java example.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Running Code Snippets
You want to test just a section of code, without having to get a whole program to run.
There's another convenient way to run Java code in Eclipse: you can use a scrapbook page. Scrapbook pages enable you to execute code, even partial programs, on the fly. Using scrapbook pages isn't an essential skill in Eclipse, but it's a useful thing to know.
After you've created a scrapbook page, you can copy and paste code to that page and run it. Create a scrapbook page by selecting File New Scrapbook Page. Enter a name for the page, such as ScrapPage, in the File box, and click Finish. The new scrapbook page is stored in the Package Explorer as ScrapPage.jpage, as shown in Figure 1-14.
Figure 1-14: Using a scrapbook page
Enter the code you want to run in this new page. For instance, to run the example code in the FirstApp project we just ran, enter the following code in the scrapbook page; note that to reach the main method of our example, you have to qualify its name with the name of the package it's in:
               String[] args = {};
               org.cookbook.ch01.FirstApp.main(args);
            
You select the code you want to run in the scrapbook page by highlighting it. In this case, select all the code in the scrapbook page, right-click it, and click Execute, or select Run Execute. (If you need imports for the code in the scrapbook page, right-click the scrapbook's code, and select Set Imports in the context menu.)
Execute runs the code in a scrapbook page, but another option also exists. If you select Display instead, the net return value of the code you've selected appears in the scrapbook. This technique is useful if you don't want to stud your code with println statements.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Fixing Syntax Errors Automatically
Eclipse indicates that you've got syntax errors in your code, but you don't want to have to loop up the correct syntax in the Java documentation.
Let Eclipse's Quick Fix suggest solutions. When you see an error/light bulb icon in the marker bar to the left of the JDT editor, click it for solutions to the error, and select the solution you want.
Quick Fix is a great tool; as far as I am concerned, if Eclipse did nothing else, it still would be a worthwhile program to use just because of its Quick Fix feature. Of all the built-in components of the JDT, Quick Fix is a real favorite among programmers because it enables you to fix syntax errors almost instantaneously.
For instance, say you want the example code we've developed in the previous few recipes to also display the date and time. You might use code such as this:
public class FirstApp {
    public static void main(String[] args) {
        Date date = new Date( );
        stayCoolText = "Stay cool.";
        System.out.println(stayCoolText + " Date now is " + date.toString( ));
  }
}
You might spot a few errors here; Eclipse certainly does, as indicated by the X icons in the marker bar to the left of the editor in Figure 1-15.
Figure 1-15: Using Quick Fix
Although Eclipse indicates that errors exist, it doesn't leave you in the cold. If Eclipse has a Quick Fix solution to offer, a light bulb will appear next to the X icon, also shown in Figure 1-15.
Tackle the first error by letting the mouse rest over the icon in the marker bar, which brings up a tool tip indicating that the Date class can't be resolved, as shown in Figure 1-16.
Figure 1-16: Getting the description of an error
To start Quick Fix and see what the JDT suggests, click the light bulb icon. You can see those suggestions in Figure 1-17; note that Quick Fix also indicates the code it's proposing to add. Double-click the import 'java.util.Date' suggestion to import the Date class so that it can be resolved.
Figure 1-17: Quick Fix suggestions
That resolves the first problem in the code. The next problem is that the variable
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Keeping Your Workspace Clear
As you create more projects, your workspace gets cluttered. Eventually, the Package Explorer contains dozens of projects, old and new, and you've got to scroll to get to the ones you want.
To remove a project from the Package Explorer, simply delete it.
Deleting a project does not necessarily delete the actual files used for the project. When you want, you can add the project back. For example, to remove the FirstApp project, just right-click it, and click Delete. Eclipse will display the Confirm Project Delete dialog box, as shown in Figure 1-20.
Figure 1-20: Removing a project
Make sure the "Do not delete contents" radio button is selected, and then click Yes to remove the project from Eclipse. The project will disappear from the Package Explorer.
Clicking the other radio button in this dialog will make Eclipse delete all the files and their contents in the project, so don't do that if you want to use the project again later.
If you want to work with the project again, just import it back into the workspace. Right-click the Package Explorer and open the Import dialog by selecting Import from the context menu, or by selecting File Import. Select Existing Project into Workspace and click Next. In the next pane, click the Browse button, select the FirstApp folder, and click OK.
Click Finish to import the FirstApp project again. The project reappears in the workspace. Removing projects from the workspace like this and importing them again as needed is the easiest way to reduce workspace clutter. We'll take a longer look at this issue in Chapter 3, when we start handling working sets.
Recipe 3.17 on creating working sets.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Recovering from Total Disaster
Your installation of Eclipse has become hopelessly fouled up.
Just save any of the projects from the workspace directory that you want to retain, delete the decompressed files and directories, and unzip or untar the Eclipse download again. After replacing the saved projects in the workspace directory (omitting any suspect projects that might have caused the problem in the first place), you're set.
One reason people like Eclipse is because they have control over the (re)installation process. This is in stark contrast to other IDEs, some of which are massive and do so many things behind the scenes that you can never trust them.
Note that you also can migrate projects from one version of Eclipse to another simply by copying project folders into the new version's workspace directory. This doesn't always work with major revisions of Eclipse, but with minor revisions, it works like a charm.
Eclipse doesn't install itself using the Windows installer, so there's no need to use the Windows control panel's Add/Remove Programs entry to manage Eclipse installation.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Using Eclipse
This chapter is about mastering Eclipse in everyday use. Chapter 1 gave you the basics; this chapter is designed to give you a working knowledge.
As with any complex tool, you might want to change things about Eclipse as you work with it more and more. So, in addition to teaching you how to work with Eclipse, this chapter also teaches you the many ways in which you can customize Eclipse, from moving views to creating your own perspectives.
When it comes to customizing Eclipse, one dialog stands out over the rest: the Preferences dialog, which you open by selecting Window Preferences. This dialog is shown in Figure 2-1.
Figure 2-1: The Preferences dialog
I encourage you to become familiar with this dialog. The Preferences dialog is the home of Eclipse customization, especially workbench customization. Want to automatically close all editors when you exit Eclipse (which enables Eclipse to start more quickly next time)? Select Window Preferences Workbench Editors and then check the "Close all editors on exit" checkbox. Want to make editor tabs appear on the bottom of editor windows? Select Window Preferences Workbench Appearance, and click Bottom in the Editor tab position box. Want to specify what editor or program Eclipse should use to open files with a certain file extension? Select Window Preferences Workbench File Associations, choose a file extension, and click the Add button. Problem solved.
But as powerful as the Preferences dialog is, it's just the beginning of the customization story. Eclipse can be customized in thousands of ways. Read on to get a grip on tailoring Eclipse to match your requirements.
Where did the Console view go? It was here a minute ago.
To show a view, select Window Show View, and choose the view you want to show. If the view you want isn't visible, select Window Show View Other, and choose a view from the dialog box that Eclipse presents of all views it knows about. To close a view, click the X button in its tab.
It's easy to close a view accidentally or to work in a perspective that doesn't display a favorite view, such as the Console view that displays text sent to the output console. Just open the view by selecting Window
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
This chapter is about mastering Eclipse in everyday use. Chapter 1 gave you the basics; this chapter is designed to give you a working knowledge.
As with any complex tool, you might want to change things about Eclipse as you work with it more and more. So, in addition to teaching you how to work with Eclipse, this chapter also teaches you the many ways in which you can customize Eclipse, from moving views to creating your own perspectives.
When it comes to customizing Eclipse, one dialog stands out over the rest: the Preferences dialog, which you open by selecting Window Preferences. This dialog is shown in Figure 2-1.
Figure 2-1: The Preferences dialog
I encourage you to become familiar with this dialog. The Preferences dialog is the home of Eclipse customization, especially workbench customization. Want to automatically close all editors when you exit Eclipse (which enables Eclipse to start more quickly next time)? Select Window Preferences Workbench Editors and then check the "Close all editors on exit" checkbox. Want to make editor tabs appear on the bottom of editor windows? Select Window Preferences Workbench Appearance, and click Bottom in the Editor tab position box. Want to specify what editor or program Eclipse should use to open files with a certain file extension? Select Window Preferences Workbench File Associations, choose a file extension, and click the Add button. Problem solved.
But as powerful as the Preferences dialog is, it's just the beginning of the customization story. Eclipse can be customized in thousands of ways. Read on to get a grip on tailoring Eclipse to match your requirements.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Showing/Hiding Views
Where did the Console view go? It was here a minute ago.
To show a view, select Window Show View, and choose the view you want to show. If the view you want isn't visible, select Window Show View Other, and choose a view from the dialog box that Eclipse presents of all views it knows about. To close a view, click the X button in its tab.
It's easy to close a view accidentally or to work in a perspective that doesn't display a favorite view, such as the Console view that displays text sent to the output console. Just open the view by selecting Window Show View.
For example, to add the Navigator view to the Debug perspective, open the Debug view (covered in Chapter 5) by selecting Window Open Perspective Debug. Then add the Navigator view by selecting Window Show View Other Basic Navigator.
If you want to save the newly configured perspective, select Window Save Perspective As. For more information, see the Recipe 2.21 later in this chapter.
Some perspectives try to stack too many views on top of each other, which can make scrolling to the right tab annoying. Now that you know you can open views again as needed, you should have no qualms about closing extra views to remove clutter.
Recipe 5.3 on starting a debugging session; Chapter 1 of Eclipse (O'Reilly).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Moving a View or Toolbar
The Package Explorer should be on the right, shouldn't it?
You can drag toolbars and views in Eclipse, and they'll dock on the various edges of the Eclipse window. Figure 2-2 shows the Package Explorer being dragged to a new location.
Figure 2-2: Dragging a view
The Package Explorer now appears on the right, as shown in Figure 2-3.
Figure 2-3: Repositioning the Package Explorer
Besides being dragged, toolbars also can be broken into segments. Each toolbar features a graspable handle (the upright 3D bar at the left edge of the toolbar). By dragging this handle, you can resize the segments in the toolbar, as well as show or hide additional controls.
You might not like working in an environment in which you can accidentally move toolbars, so select Window Lock the Toolbars to hold things in place. If a perspective becomes scrambled as a result of accidental mouse movements, select Window Reset Perspective to reset the perspective.
You also can drag editors, but you can't mix items in the editor and view areas. The editor area is the center of the Eclipse window, and Eclipse won't allow you to drop any views there, or drop an editor on top of a view.
Chapter 1 of Eclipse (O'Reilly).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Accessing Any Project File
Many perspectives will hide files; you want to get access to a specific file (or files) in your project.
The Navigator view in the Resource perspective gives you access to all the files in a project, without exception.
Some perspectives hide files. For example, Eclipse stores project information in an XML file named .project, but many views, such as the Java perspective's Package Explorer, will hide that file. The .project file for a sample project open in the Resource perspective is shown in Figure 2-4.
Figure 2-4: Using the Resource perspective
Some Java-oriented perspectives also support the Navigator view. Many Java programmers ignore the Resource perspective completely, but some Java-oriented views will hide various files from you. To get access to them all, don't forget about the Navigator view; it's always available in the Resource perspective as well as in some of the Java perspectives.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Tiling Editors
Although you can switch editors by clicking their tabs in the editor area, sometimes it's more desirable to have two editors open at once, e.g., when you're comparing two files visually.
You can drag editors and can tile the editor area with them.
When you drag an editor and position it near an edge of the editor area, you'll see an arrow, as shown in Figure 2-5, which indicates that dropping the editor will dock it on that edge.
Figure 2-5: Dragging an editor
The new, tiled editor presentation is shown in Figure 2-6.
Figure 2-6: Tiling editors
To restore an editor to its original position, just drag it back to where it was.
You can open an item in an editor by dragging the item from a view such as the Package Explorer and dropping it on the editor area.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Maximizing Views and Editors
The Eclipse window is crammed with menu bars, toolbars, views, and editors. Sometimes, editing your code seems too cramped an experience.
You can maximize views or editors simply by double-clicking the view's titlebar or the editor's tab, something few people know.
Figure 2-7 shows a maximized view of the JDT editor.
Figure 2-7: Maximizing an editor
Double-click the view's titlebar or the editor's tab to restore it to its original size.
As with many IDEs, Eclipse can feel crammed at times, especially when you're working on large documents. You might end up feeling like you're peering through a nest of distractions at the code you need to edit. To solve this problem, just double-click the editor's tab to maximize the editor.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Going Back to the Previous Editor
You've got 20 editors open, and scrolling through all those editor tabs to navigate between them is taking a long time.
Use the workbench editor's navigation history, which works much like a web browser's history. Just click the back arrow in the workbench toolbar as you would in a web browser to go back to the previous editor. (Or use Navigate menu items such as Navigate Backward, or keyboard shortcuts such as Alt-Left Arrow.)
When you work with Eclipse on extended projects, you'll find the editor area filling up with editors. And as the editors get more cramped, Eclipse makes their tabs smaller and the text in them more abbreviated. The more editors you have open, the more difficult it becomes to move between them. One way to ameliorate this problem is to bear in mind that you can use navigation controls to move between recently used editors.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Going Back to the Previous Edit Location
While editing, you switched to another editor to make sure that you had the name of a variable right. But you have 15 editors open. How do you get back to the exact line of code you were editing?
Select Navigate Go to Last Edit Location (Ctrl-Q), which moves you back to the location where you last made a change in an editor. You'll also see a button for this function in the toolbar when working with editors.
Eclipse lets you easily move between edit locations, a valuable addition to any programmer's toolset. If you can't see these buttons in your current perspective, you might need to add them by selecting Window Customize Perspective Other Editor Navigation).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Linking Views to Editors
The items selected in a view are not tied closely to whichever editor is open. Switching editors, or even closing an editor, does not change the selection in a view by default.
Click the Link with Editor button.
Many views, such as the Resource perspective's Navigator view and the Java perspective's Package Explorer, enable you to synchronize with editors. To synchronize, click the Link with Editor button shown in Figure 2-8.
Figure 2-8: The Link with Editor button
By linking a view with an editor, you tie that view to the editor so that the view will always show the file currently being edited.
Chapter 1 of Eclipse (O'Reilly).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Reordering View and Editor Tabs
You've got more than a dozen editors open, and the two you're working with are at opposite ends of the stack. As a result, you must scroll back and forth over all the editor tabs to work with the two editors you need. Isn't there a better way?
You can rearrange the order of the view and editor tabs as you prefer in Eclipse. Just drag the tab(s) to the order you want.
This is a useful skill that few Eclipse developers know about, even those who use Eclipse everyday. When you open a large number of editors, as when you're working with multiple projects, you might find yourself surfing the editor tabs to find the ones you want. To avoid that, you can drag and drop those tabs in the order you want, grouping the editors you're currently worki