4.4. Searching Code


You want to search a single file, or all files in a project, for some matching text.


Use the built-in Eclipse Search dialog. The Search menu contains multiple items (Search Search, Search File, Search Help, and Search Java), but all of them open the same dialog.


As you’d expect of a good IDE, Eclipse has a lot of built-in search support. However, searching in Eclipse is not accomplished via the standard Edit Find/Replace operation, which enables you to search the current file. Instead, searching displays all matches in the Search view so that you can select the one or ones you want to jump to. In addition, the items in the Search menu enable you to search across all files in workspaces, through the help system, all plug-ins, and more. Wildcards (* and ?) work as well.

The Search dialog appears in Figure 4-7; note the four tabs there:

File Search

Searches specified files in a working set, the workspace, or files you select.

Help Search

Searches the help system for matches.

Java Search

Searches the workspace, a working set, or selected resources for matches. You can specify what you’re searching for: type, method, package, constructor, or field.

Plug-in Search

Searches for plug-ins, fragments, and extension points.

The Search dialog

Figure 4-7. The Search dialog

For example, take a look at the File Search tab, which enables you to search across multiple files for specified text (printem in this example). You can give the pattern(s) of the files to search, such as "*.java, *.*“, and so on, in the “File name patterns” box, and set the scope of the search with the radio buttons beneath that box.


The “Selected Resources” radio button in the “Scope” box is not enabled in this dialog by default. If you want to search only the current project or just a restricted number of projects without using a working set, select that project or those projects in the Package Explorer, then open the Search dialog and click the “Selected Resources” radio button.

When you click the Search button, the results appear in the Search view, shown in Figure 4-8. Double-clicking a match opens the match in the JDT editor with an arrow in the marker bar, as shown in the figure.

Search results

Figure 4-8. Search results

You also can perform Java searches with the Java Search tab in the Search dialog. This kind of search enables you to search for Java elements by kind—types, methods, packages, constructors, and fields, as shown in Figure 4-9. You can limit the search so that it matches only references, also shown in the figure.

Performing a Java search

Figure 4-9. Performing a Java search


To search for methods with a specific return type, open the Search dialog, and click the Java Search tab. Type * < return type > in the Search string, click the “Method” and “Declarations” radio buttons, and then click Search.

Being able to perform multifile searches such as this is one of the biggest reasons for using an IDE. If you’ve been writing Java with a simple text editor, you’ll find there’s no comparison.

Besides the Search Search, Search File, Search Help, and Search Java items in the Search menu, you also can highlight an element in your code and select the following Search menu items to do a quick search:

Search References

Finds references to the element

Search Declarations

Finds the element’s declaration

Search Implementors

Finds implementors of the element

Search Read Access

Finds read accesses (fields only)

Search Write Access

Finds write accesses (fields only)

These items are handy for tracking down all references to a method or variable, for example; just search for the references, and double-click the results in the Search view. To limit the scope of the search, select Workspace, Hierarchy, or Working Set from each item’s submenu.


Another quick way to search for references is to highlight an identifier and select Search Occurrences in File. This is more useful than Edit Find/Replace because it lists all matches in the Search view, enabling you to navigate to the ones you want.

Eclipse 3.0

In Eclipse 3.0, you also can click a radio button to automatically limit the searches to the enclosing project in the File Search, Java Search, and Plug-in Search tabs. Plans are in the works to enable you to use regular expressions in searches as well.

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.