Home > Articles > Programming > Java

Using Java Development Tools in Eclipse

Make writing Java code quick, fun, and productive with the Java Development Tools (JDT) in Eclipse. Explore all that the JDT has to offer you through a Java debug perspective, a Java project definition, editors, views, wizards, refactoring tools, and much more.
This chapter is from the book

Eclipse provides a first-class set of Java Development Tools (JDT) for developing Java code. These tools include a Java perspective, a Java debug perspective, a Java project definition, editors, views, wizards, refactoring tools, a Java builder (compiler), a scrapbook for evaluating Java expressions, search tools, and many others that make writing Java code quick, fun, and productive.

The JDT views and editors provide an efficient and effective way to quickly and intuitively navigate through Java source code and view Javadoc. The Java editor supports syntax highlighting, content assist, error cluing, state of the art refactoring, type-ahead assistance, and code generation among a host of other capabilities. The Java compiler is an incremental compiler; it compiles only what it must based on your changes. It supports JDK 1.3 and 1.4. JDT can be configured with different JREs. You can develop code with one JRE and debug with another.

The Java editor is more than a source code editor. It builds and maintains indexed information about your code, enabling you to quickly search for references to classes, methods, and packages. The incremental compiler is constantly running in the background and will alert you to potential errors in your code before you save it. In many cases JDT will propose several solutions to the problems it detects, such as adding an import statement that was omitted, suggesting typographical corrections, or even creating a new class or interface. This allows you to focus on your code and not be distracted by compiler demands.

In the following sections we're going to look in more detail at the capabilities and use of the Java tools. We'll start with an overview of the JDT user interface and the fundamentals you'll need for creating and navigating Java resources. We'll then look in much more detail at JDT capabilities for coding Java. Then we'll see how to run Java code. In the final sections of this chapter we'll get into more detail on working with Java elements, tuning the performance of JDT, and specific functions in the JDT views and perspectives.

Getting Started

Let's go. We'll start with a quick overview of the JDT user interface. We'll then cover the fundamentals, like opening a class, navigating to a method or field, and running a Java program. We'll also discuss how to search Java code.

Overview of the JDT User Interface

The Java perspective is the default perspective for Java development and the one that comes up when you create a new Java project (see Figure 3.1).

Figure 3.1Figure 3.1 Java Perspective


The left pane contains the Package Explorer view and Hierarchy view. The Package Explorer view does for the Java perspective what the Navigator view does for the Resource perspective. Use the Package Explorer view to navigate in your Java projects, perform operations on resources, and open files for editing. The middle pane contains open editors, Java and other- wise. The active editor is the one on top. In Figure 3.1, an editor has PrimeNumberGenerator.java open in it. The right pane is the Outline view, which presents a structured, hierarchical view of the contents of the active editor. On the bottom right is the Tasks view, the same one we saw in the section "Working with Tasks" in Chapter 2. As you navigate in the user interface, selecting Java files for editing and selecting Java elements, all the views and the editor stay synchronized with your actions.




The Java views display Java elements with icons for a package and for public methods. Some of these icons are decorated to provide further information with overlays, such as to indicate a class has a main method or to indicate a method overrides a method in a superclass. For a complete list of the JDT icons and decorations, refer to the "Icons" section in the "Reference" section in the Java Development User Guide.


You have two options for how code appears in the editor as you navigate in the user interface. Show Source of Selected Element Only on the toolbar controls this. The default is to show the contents of the entire file. If you prefer to focus on smaller portions of code, toggling this option will only show the source for the selected class, method, field, or import statement. This is mostly a matter of personal preference.

The Fundamentals

Here are the fundamental tasks you need to understand to create and navigate through Java resources. These are available when you are in one of the Java perspectives.

Creating a Java Project

All Java elements must exist in a Java project. To create a Java project, select File > New > Project... > Java Project from the menu or select Create a Java Project from the toolbar.

When creating Java projects and other elements, if you get the names wrong or later decide you want to change them, the JDT refactoring capabilities make it easy to rename elements and update references to them (refactoring will be discussed in more detail later in this chapter).

Creating a Package


Java types, that is, classes and interfaces, must exist in a package. If you do not create one, a default will be created for you. To create a package, select the containing project and select File > New > Package from the menu or select Create a Java Package from the toolbar.

Creating a Type

To create a type, select the containing project or package and select File > New > Class or File > New > Interface from the menu or select Create a Java Class or Create a Java Interface from the toolbar.

Opening a Type

To open a Java class or interface, do one of the following.

  • Double-click on a Java source file, class, or interface, or select one of these in a view and press F3 or Enter. You can do this from any Java view.

  • From the editor, select the name of a class or interface in the source code (or simply position the insertion cursor in the name), and then select Open Declaration from the context menu or press F3.

  • Select Ctrl+Shift+T and enter the name of a class or interface in the Open Type dialog.

Opening a Method or Field

To open a method or field definition, do one of the following.

  • Double-click on a method or field, or select a method and press F3 or Enter to see its definition. You can do this from any Java view.

  • From the editor, select the name of a method or field in the source code (or simply position the insertion cursor in the name) and then select Open Declaration from the context menu or press F3 to see its definition.

Viewing Supertypes and Subtypes

To view the supertypes or subtypes for a class or interface in the Hierarchy view, do one of the following.

  • Select a Java element, such as a Java source file, class, method, or field, and then select Open Type Hierarchy from the context menu or press F4. You can do this from any Java view.

  • From the editor, select the name of a Java element in the source code (or simply position the insertion cursor in the name) and then select Open Type Hierarchy from the context menu or press F4.

  • Select Ctrl+Shift+H and enter the name of a class or interface in the Open Type in Hierarchy dialog.

Navigating to a Type, Method, or Field

You can navigate to class, interface, method, and field definitions in your Java code simply by selecting one of these elements in a view, such as the Outline or Type Hierarchy view. The editor and open views scroll to your selection.

NOTE

If you select an element and you do not see it in an editor, it means the file containing the definition is not open. You must then first open the file using one of the methods described above.

Locating Elements in Projects

As you use the views and editor to navigate in your code, you can locate element definitions in projects by selecting a Java element in one of the views or the editor (or positioning the insertion cursor in the element name) and then selecting Show in Package Explorer from the context menu. The Package Explorer scrolls to the selected element. The element must be defined in an open project in your workspace.

Running a Java Program

To run a Java program, select a class with a main method and then select Run > Run As > Java Application from the menu. Output is shown in the Console view.

Searching

There are two types of searches: a general Eclipse file search for text strings, and a Java-specific search of your workspace for Java element references. The search capability uses an index of Java code in your workspace that is kept up-to-date in the background, independent of Java builds. This means that you don't have to have the auto-build preference selected or save your modifications in order to do a search.

Searching a File

To search the file in the active editor for any text, select Edit > Find/Replace, or press Ctrl+F. Eclipse also has an "incremental find" feature that provides a keystroke-efficient way to do this. Select Edit > Incremental Find from the menu or press Ctrl+J and note the prompt in the message area to the left on the bottom margin (see Figure 3.2). Start typing the text you're searching for. The message area displays the text you're searching for and the first match is selected in the editor. Press Ctrl+K to find the next occurrence of the text.

Figure 3.2Figure 3.2 Incremental Find


Searching Your Workspace

Select Search or press Ctrl+H and then select the Java page to search your entire workspace, or a subset of it, for references to Java elements. There are three aspects to a Java search: what kind of reference, what kind of Java element, and within what scope. You can specify these by using Limit To, Search For, and Scope, respectively, in the Search dialog, as shown in Figure 3.3. To restrict a search, first select one or more Java elements in the Package Explorer, Outline, or Type Hierarchy view and then select Search, or define a Working Set and then specify that working set under Scope. (For more information on Working Sets, refer to the section "Working Sets" in Chapter 2.) For example, if you want to search for methods returning void in your projects, select the projects in the Package Explorer view, select Search, specify the Search string "* void", select Search For Method and Limit To Declarations, select Selected Resources, and then select the Search button (or press Enter).

Figure 3.3Figure 3.3 Searching for Java Elements



Java search results are shown in the Search view (see Figure 3.4). Matches are indicated in the editor with entries on the marker bar. Navigate to matches from the Search view by double-clicking an entry or by selecting Show Next Match and Show Previous Match.

Figure 3.4Figure 3.4 Search View

In all of the JDT views, including the Search view and the editor, you can select an entry or Java element and then search on that element from the context menu by selecting References > or Declarations >. This ability to successively search for Java elements from the views, especially the Type Hierarchy and Search views, provides a simple, efficient way to explore and understand Java code.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020