This article talks about the Java™ Development Tools (JDT), illustrating some commonly used features and explaining how to customize them. It also discusses concepts and features that may be different in the Eclipse integrated development environment (IDE) vs. others, or features that may not be apparent to new users of the JDT. See Part 1 if you're just getting started with Eclipse V3.4.
Eclipse has become synonymous with Java development. However, to many Eclipse newcomers, the relationship between Eclipse and Java technology may not be entirely clear.
Eclipse is a bare-bones platform that happens to be written in the Java programming language. Plug-ins, such as Mylar and Findbugs, written on top of the IDE provide its functionality, and the Java language allows Eclipse to function like a Java IDE. Possibly because the JDT is included by default in the Eclipse downloads, it may not be immediately clear that when developers say they use Eclipse for Java development, what they really mean is that they use the JDT.
Figure 1. A typical Java project as viewed from the Package Explorer
A Java project consists of the following elements:
- src folder
- This contains the actual source code of your application. By default, when you create a new Java project, Eclipse creates a src folder in which to keep all your source code, but you can add any folder as your source folder. For example, many people like to have a tests folder in which they create all their test cases. To add a tests folder to your project:
- Right-click the project, then click New > Folder.
- Type a name for the folder (
tests, in this case) and click OK. - Right-click the tests folder, then click Build Path > Use as Source Folder.
- output folder
- This contains the generated .class files from your source code. By default, Eclipse creates a bin folder to hold these.
- Referenced libraries
- These are libraries or other Java projects in the workspace on which your current project depends. They are added to the build path and CLASSPATH when Eclipse is building or running your Java project.
Many times, Java projects use the classes located in some other Java archive (JAR) on your computer or in some other project in the same workspace. To use these classes, you must add the corresponding projects/JARs to the build path of your current project. Otherwise, Eclipse shows compile errors in your project. To modify a project's build path:
- Right-click the project in the Package Explorer and click Settings.
- In the resulting window, select Java Build Path.
- To add projects in the same workspace as your build path, click the Projects tab, then click Add.
- Select the projects you want on the build path, then click OK.
To add JARs to your project:
- Click the Libraries tab.
Figure 2. Modifying the build path
- Click Add external JARs, then select the JARs you want to add.
Eclipse lets you customize the settings of its built-in Java compiler. Eclipse uses this compiler to build your project. To customize the compiler settings for your project:
- Right-click the project in the Package Explorer and click Settings.
- In the resulting window, select Java Compiler.
Figure 3. Customizing Java compiler settings
- On this and the rest of the preference pages for the Java compiler:
- Select Enable project specific settings so the changes you make to the compiler settings apply to this project only.
- Click the Configure Workspace Settings link to make your changes apply to all the projects in the workspace.
- From the Compiler compliance level menu, choose the Java version to which you want to compile your sources. For example, if you're using Java V1.5 features, you should select that option.
- Expand the Java Compiler tree, and then select Errors/Warnings. Here, you can set Eclipse to flag potential errors in your code. If you find an item too strict, you can tell Eclipse to ignore it (for example, Undocumented Empty Block), or you can set the items to flag a warning or error depending on how severe you think the problem is.
Figure 4. Customizing Java compiler errors and warnings
Note: It's recommend that you set the items under Potential programming Problems and Deprecated and Restricted API to at least the Warning level. You may want to ignore the Serializable class without serialVersionUID item under Potential programming Problems unless you plan to synchronize many of your objects. - Select Task Tags in the expanded Java Compiler tree. Task tags let you leave notes to yourself or to other developers in code comments. For example, you can have a line like this:
//TODO this is a bug. details at http://bugs.mycompany.com/3434/You can see these notes by themselves in the Tasks view. Clicking any note in the Tasks view jumps to it in the source file. By default, Eclipse offers three types of task tags:FIXME,TODO, andXXX. You can add your own tags, such asBUG, simply by choosing Task Tags in the Java Compiler tree (see Figure 5), clicking New, and typing the name of the tag in the window that appears. You can even set a priority level for the tag, which is useful because you can sort the notes in the Tasks view by priority.
Figure 5. Customizing task tags
Refactoring lets you easily make changes to your code in one place and have those changes show up in other places that code appears. If that sounds confusing, hold on: This section looks at what is probably the most exciting part of Eclipse. Because Eclipse contains many refactorings and describing them all would take an article of its own, this article concentrates on some that are most frequently used.
Rename refactoring is probably the most commonly used of all the refactorings. You can use it to rename any method, variable, or class in your project. To try it:
- Select any variable in the editor.
- Click Refactor > Rename from the menu at the top of the Eclipse GUI, shown in Figure 6). The Java editor places boxes around the variable name and all the places where it's used.
Figure 6. Refactor menu offers many refactorings
- Type a new name for the variable, and then press Enter. All the places where the variable is used are automatically changed to use the new name.
Similarly, you can change the names of classes and methods. All the places they're used will be changed to use the new names. This way, you don't have to manually locate every instance in which a method is called and change it, which saves you a lot of time and prevents potential errors. You can easily change the name of any methods, variables, and classes in your Java projects without giving it a second thought.
This type of refactoring is especially useful in moving classes from one package to another. You'll probably use it quite often — second only to rename refactoring. To try it:
- Right-click any class in the Package Explorer, and then click Refactor > Move. A window opens, showing the packages in the current project and any other project that this project depends on.
- Select the package to which you want to move the class, then click OK. The class is moved to the new package physically, and all references to it in the code — such as
importstatements — are changed to refer to its new location.
If you'd like to skip these steps, you can drag and drop a class from one package to the other. The move refactoring automatically takes place.
Eclipse doesn't have a Compile button. This throws off many newcomers, but it's another amazing feature of Eclipse. You never need to compile a Java project because the project is always built. Whenever you save a file, Eclipse compiles it and all the other files affected by it in the background, so you never notice it. This behavior reduces the amount of time required to launch your project because you never have to wait for it to recompile.
The simplest way to run a Java project is to open the file containing the main method, then click Run on the toolbar. Doing so creates a launch configuration. The next time you want to run the project, click the arrow next to Run, as shown in Figure 7 and select the configuration with the name of your class containing the main method.
Figure 7. Run a project
You should start debugging by placing breakpoints in your code. You can do so by clicking the leftmost column in the editor, adjacent to the line on which you want the place the breakpoint. Starting in Debug mode is similar to running your project except that you click Debug on the toolbar, rather than Run.
Figure 8. Debug a project
When you hit a breakpoint, the Eclipse window pops to the foreground and automatically switches to the Debug perspective, which contains views to help you debug the program. Here's a brief description of some of the commonly used views:
- Debug view
- This view controls the currently running program, allowing you to view the stack and step through the program. You can even use this view to pause or stop the program.
- Variables view
- This view shows the local variables in the current method. Values change as you step through the program.
- Breakpoints view
- This view lists the current breakpoints. You can enable or disable breakpoints by selecting or clearing them in the list.
- Expressions view
- This view lets you type any arbitrary Java expression and see its value in the context of the current point in the program's execution. To add Java expressions to this view:
- Right-click inside the view, and then click Add expression.
- In the window that appears, type your expression and click OK. The expression is added to the Expressions view, and its value is updated as you step through your code. Keep in mind that typing an expression that modifies the value of any variable may have an unintended effect on your code.
This article examined the Eclipse JDT and the features it offers. You now know how to customize portions of the JDT to your liking, and you should understand a few things that can confuse newcomers to the JDT.
Part 3 of this "Mastering Eclipse" series focuses on the most powerful part of the JDT — its editor — and examines its powerful capabilities.
Learn
-
Read "Mastering Eclipse V3.4, Part 1: The Eclipse workbench" if you're new to Eclipse.
-
Find more information in the Workbench User Guide in Eclipse Help, an Eclipse Foundation resource.
-
Check out the Eclipse IDE Pocket Guide.
-
Visit the official Eclipse FAQs.
-
Read Eclipse for Dummies, by Barry Burd.
-
Be sure to read Eclipse Distilled, by David Carlson.
-
Check out the "Recommended Eclipse reading list."
-
Browse all the Eclipse content on developerWorks.
-
New to Eclipse? Read the developerWorks article "Get started with Eclipse Platform" to learn its origin and architecture, and how to extend Eclipse with plug-ins.
-
Expand your Eclipse skills by checking out IBM developerWorks' Eclipse project resources.
-
To listen to interesting interviews and discussions for software developers, check out check out developerWorks podcasts.
-
Stay current with developerWorks' Technical events and webcasts.
-
Watch and learn about IBM and open source technologies and product functions with the no-cost developerWorks On demand demos.
-
Check out upcoming conferences, trade shows, webcasts, and other Events around the world that are of interest to IBM open source developers.
-
Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
Get products and technologies
-
Check out the latest Eclipse technology downloads at IBM alphaWorks.
-
Download Eclipse Platform and other projects from the Eclipse Foundation.
-
Download IBM product evaluation versions, and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
-
Innovate your next open source development project with IBM trial software, available for download or on DVD.
Discuss
-
The Eclipse Platform newsgroups should be your first stop to discuss questions regarding Eclipse. (Selecting this will launch your default Usenet news reader application and open eclipse.platform.)
-
The Eclipse newsgroups has many resources for people interested in using and extending Eclipse.
-
Participate in developerWorks blogs and get involved in the developerWorks community.

Prashant Deva is the founder of Placid Systems and the author of the ANTLR Studio plug-in for Eclipse. He also provides consulting related to ANTLR and Eclipse plug-in development. He has written several articles related to ANTLR and Eclipse plug-ins, and he frequently contributes ideas and bug reports to Eclipse development teams. He is currently busy creating the next great developer tool.



