Agile Developer’s Toolbox: Checkstyle for Eclipse
If you use Eclipse for day to day Java development take a deep breath and relax. You are neither the first nor the only one. In fact, you are part of a growing community of Java developers in the world. SD Times recently reported that two-thirds of the enterprise software developers—66.3 percent—use Eclipse. This adoption rate is a success for an open source project, albeit supported by a giant like IBM.
The agile developers know that at the end it’s often not the tool that makes the difference. It’s the fundamental engineering practices. When the discussion goes beyond the tools it suddenly opens so many possibilities that even the most staunch tools proponents/opponents can sit down and unite in a common strategy. Let’s take coding standards for example. We all know that there are tons of tools for static code analysis and early defect detection. The Agile teams have choices in this matter. They can implement a set of static code analysis as part of their Continuous Integration (CI) cycle and get great reports the instant a potential bug gets checked-in. They can also encourage individual developers to use Eclipse plugins for code analysis and perform the same checks before they commit the code.
Two major plugins for code analysis in Eclipse are PMD and Checkstyle. Of course, Eclipse itself is also catching-up and it is constantly increasing its own set of code style checks. A look at Window | Preferences | Java | Compiler | Errors/Warnings reveals that the number and depth of the built-in code checks is fairly substantial.
Nevertheless, in this blog entry I would like to show you a quick example of how to setup and use Checkstyle for Eclipse .
Despite the fact that I use PMD and Checkstyle (and others for that matter, like Findbugs) in my daily projects, I favor Checkstyle for the following reasons:
-
The integration of the tool to the environment seems to be tighter
-
The rule customization and management in Checkstyle is way easier than PMD. I like!
-
Checkstyle provides a few nifty features such as a pie chart depicting the distribution of problems
-
Checkstyle comes with a richer set of predefined rules
-
Better website documentation and easier learning curve
All and all Checkstyle makes my life easier, and especially after configuring a set of rules that make the sense for the project environment, it’s a workhorse! An added benefit of the custom rulesets is that they can be checked into the code repository, shared among the development team and reused in the CI static analysis phase. The latter can be achieved through integrating tools such as CruiseControl, Maven and Ant with the respective Checkstyle plugins. This brings a unified coding standard to the entire development team.
Quick Tutorial
-
Download and start Eclipse 3.2
-
Navigate to Help | Software Updates | Find and Install
-
Select Search for new features to install
-
Add a new remote site Checkstyle and point it to http://eclipse-cs.sourceforge.net/update
-
Finish the wizard and restart Eclipse
-
Create a new Java project, checkmeup
-
Add the following Java class:
public class CheckMeUp { public void thisMethodShouldHaveAShorterAndCoolerName(String WHYALLCAPS, int g) { if (true) { } else { } while (true) { String x = "The white fox jumped over the red rabbit, which in turn jumped over my yellow submarine."; String y = x + "..."; } } } - Build the project
- In the Package explorer right-click on checkmeup and select Properties | Checkstyle
- Check Checkstyle active for this project and select Sun Checks (Eclipse) - (Global) as the ruleset
- Navigate to Window | Show view | Other…
- Select Checkstyle | Checkstyle violations
- Notice the 27 violations in this simple Java class
- Now, let’s play with the rules. Assume that you do not like the Line contains a tab character rule. The sample class has 12 of it. Navigate to Window | Preferences | Checkstyle and Click on New…
- Select Internal Configuration, enter a name and description. Click Import… and navigate to ECLIPSE_HOME/plugins/com.atlassw.tools.eclipse.checkstyle_x.x.x and select sun_checks_eclipse.xml. This will import all Sun Eclipse rules for the new custom ruleset. Also note that Checkstyle will create a new custom ruleset xml file under WORKSPACE/.metadata/.plugins/com.atlassw.tools.eclipse.checkstyle/internal_config__xxxxxxxxxxx.xml. This file can be added into the code repository and shared among the development team. Click OK.
- Double click the new ruleset. Click Whitespace and uncheck the Tab Character rule
- Back in the project properties, select the new custom rule as your configuration
- Click OK. The tab warnings are gone.
-
In the Checkstyle violations view click the chart icon in the upper right corner. This will open the pie chart view shown below

- Enjoy.

June 7th, 2007 at 1:43 am
Nice Post. I’ve been using plug-ins like Coverclipse, PMD, Findbugs, JDepend, Metrics, CheckStyle for quite some time now. I explained my project mates about these plug-ins. But they found it difficult to grasp the functionalities at first. I could demonstrate them very easily, but What i was missing is the writing, i.e., some sort of easy How-To document. This tutorial provides me that. Keep posting about other useful eclipse plug-ins.
June 7th, 2007 at 10:33 am
Hi Manikanda,
Kudos to you for using a nice set of plug-ins and taking the step to introducing them to your team. I find it more difficult to teach people the principles than the tools that implement them. Take code analysis for example. The reason tools like Checkstyle, PMD and Findbugs exist is because they implement a set of best practices related to complexity, architectural principles and potential bug patterns. On one level it’s easy to show someone a tool; its more time consuming to demonstrate to them what’s actually being achieved and what that means to the overall success of the project. Only after people relate practices to tools to technical and business objectives do they start to realize the potential in these tools.
Thank you again,
Levent Gurses
June 19th, 2007 at 12:31 am
Can you please explain the steps of writing own checks using checkstyle and integrating that with eclipse.
Thanks,
Rony.
July 20th, 2007 at 12:15 am
Thanks for this post, very useful.
In relation to Rony’s question above, may I draw your attention to SemmleCode, another free plugin for Eclipse (http://semmle.com) ?
In SemmleCode, it’s super-easy to define new checks of your own, as queries over the code - typically it takes only a few lines. SemmleCode ships with a large library of common checks, including metrics and J2EE conventions.
Check it out at http://semmle.com !
February 26th, 2008 at 1:37 am
Hi,
I am new to Eclipse Plugin. I followed all the steps above but I couldn’t see any checkstyle point in ‘Checkstyle violations’ window. Is there any other settings required in Eclipse ?
thanks in advance.