Skip to main content

Mastering Eclipse V3.4, Part 2: The JDT

Understand commonly used features in Java Development Tools

Prashant Deva, Founder, Placid Systems
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.

Summary:  This "Mastering Eclipse" series of articles teaches complete newcomers to Eclipse the ins and outs of the Eclipse IDE. By the end of the series, you'll be on par with advanced users. In this article, learn about working with various parts of the Eclipse JDT.

View more content in this series

Date:  11 Nov 2008
Level:  Intermediate
Activity:  6037 views
Comments:  

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.

What is the JDT?

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.


Anatomy of a Java project


Figure 1. A typical Java project as viewed from the Package Explorer
 A typical Java project as viewed from the Package Explorer

Package Explorer view

Although Eclipse contains several views in which to browse files, such as the Navigator view and the Project Explorer view, you use the Package Explorer view to navigate Java projects. This view shows the src folder for each Java project and lets you navigate the packages each src folder contains. You can expand individual Java files to see their outlines. You can also expand the Referenced Libraries item for each project to see the libraries on the project's build path.

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:
  1. Right-click the project, then click New > Folder.
  2. Type a name for the folder (tests, in this case) and click OK.
  3. 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.

Set up the build path

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:

  1. Right-click the project in the Package Explorer and click Settings.
  2. In the resulting window, select Java Build Path.
  3. To add projects in the same workspace as your build path, click the Projects tab, then click Add.
  4. Select the projects you want on the build path, then click OK.

To add JARs to your project:

  1. Click the Libraries tab.

    Figure 2. Modifying the build path
    Modifying the build path

  2. Click Add external JARs, then select the JARs you want to add.

Customize compiler settings

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:

  1. Right-click the project in the Package Explorer and click Settings.
  2. In the resulting window, select Java Compiler.

    Figure 3. Customizing Java compiler settings
    Customizing Java compiler settings

  3. 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.
  4. 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.
  5. 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
    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.
  6. 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, and XXX. You can add your own tags, such as BUG, 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
Customizing task tags


Refactoring

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

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:

  1. Select any variable in the editor.
  2. 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
    Refactor menu offers many refactorings

  3. 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.

Move refactoring

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:

  1. 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.
  2. 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 import statements — 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.


Build a Java project

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.


Run a project

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
Run a project


Debug 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
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:
  1. Right-click inside the view, and then click Add expression.
  2. 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.

Conclusion

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.


Resources

Learn

Get products and technologies

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.

About the author

Prashant Deva

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.

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=350801
ArticleTitle=Mastering Eclipse V3.4, Part 2: The JDT
publish-date=11112008
author1-email=pdeva@placidsystems.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers