PMD Eclipse Plugin Installation.

Hi am not going in deep with the theory of PMD you can google it about it.

But I am here mainly concentrating on PMD installation and its usage.

Before I start with installation let me explain what is this PMD is all about.

  • PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.
  •  It supports Java, JavaScript, XML, XSL.
  •  Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code in Java, C, C++, C#, PHP, Ruby, Fortran, JavaScript.

There are hundred of rules which support here I am listing out few basic rules which supports.

  1. Empty CatchBlock,
  2. Empty IfStmt,
  3. Empty WhileStmt,
  4. Empty  TryBlock,
  5. Empty  FinallyBlock,
  6. Empty  SwitchStatements.
  7. Empty JumbledIncrementer

Example for JumbledIncrementer:

public class JumbledIncrementerRule1 {

public void foo() {

for (int i = 0; i < 10; i++) {

for (int k = 0; k < 20; i++) { 



} } }

In the above example we are incrementing i in both the loops and we are checking for k in second loop which results in infinite loop.


Now lets start with the Plugin installation and and its usage:

·  Launch Eclipse.

·  Navigate to Help | Install New Software

·  Click Add button in the popup new popup opens up there add details for name and url



  • Click ok
  • Select latest version PMD from the list and say next it will install the PMD(it may take few minits to download the requred jar files).
  •  Once the installation is complete restart the eclipse.


Now you are ready to use PMD.

  • Right click on the project and go to PMD -> Find suspect cut and paste then click ok on popup

It will process the code and give you the result of cut and paste suspect in a new window called “CPD VIEW”.

If you don’t see this window means then you can open this by navigating to “Windows->Show windows->Others” select PMD->CPD View.

The output:

You can also check the output in text file which is created in your project folder


      Checking code violations:

      Right click on the project ->PMD->Check code

      It will list all the violations happended in “Violatons Outline” and “Voilations Overview” window.

There are five buttons in this window with different color, these are clickable to make it on and off.

    Red-Blocker viollation.

    Light Blue-Critical violation.

    Green- Urgetnt violation.

    pink- Important violation.

    Dark blue- Warning violation.

    If the button is on then violations of that category is displayed.

    If you still don’t see any viollations which are displaing then check once you included the violation list    in PMD configuration.

    Goto Windows->Preferences->PMD->Rule Configuration

    You will see a list of rules listed in the popup select the required rule and say

    Apply, OK.


            Now run the “check code” and check for violations if any.

             Violation overview window.


           Now double click on the any vilation it will show up complete details in the violation outline view.


Customizing PMD

The easiest way to begin customizing PMD is by playing with existing rules. Adding new rules is also possible as well as removing unnecessary ones, however these require more knowledge. Since experimenting with existing rules is the easiest, it makes sense to start with them.

Each PMD rule has six attributes:

  • Rule name – immutable
  • Rule implementation class – immutable
  • Priority
  • Message
  • Description
  • Example

Of these six attributes the first two are immutable – they cannot be customized by users. While Message, Description and Example are text-based properties and can accept any String data, Priority is an integer field ranging from 1 to 5.

Before starting the customization it may be a good idea to back-up existing configuration.

  1. Navigate to Window | Preferences… | PMD | Rules configuration
  2. Click Export rule set…
  3. Enter a file name e.g. pmd-rules.xml and click Save
  4. Back in the Rules configuration page scroll-down and click on SystemPrintln rule
  5. With the rule selected, click the Priority column and change the priority from Error (2) to Information (5)
  6. With the rule still selected, click the Edit rule… button on the right

·  Change the Message, Description and Example. Click OK when done

·  Click Export rule set…

·  Enter a new file name e.g. pmd-rules-customized.xml and click Save

·  Compare the two XML files – they are different

·  In Package Explorer right-click on QA Project and select PMD | Clear PMD Violations

·  In Package Explorer right-click on QA Project and select PMD | Check Code With PMD

·  Wait for PMD to finish the scan

·  If PMD Violations view is not open navigate to Window | Show View | Other… and select PMD | PMD Violations


Adding New Rules

PMD allows new rules in two formats:

1.  Java-based rules where the rule is written as a Java implementation class.

2. XPath rules where the rule is defined in an XML file.

Writing Custom PMD XPath Rules

  1. In PMD Rules configuration dialog click Add rule…
  2. This time check XPath rule. The rule implementation class will automatically change to net.sourceforge.pmd.rules.XPathRule. Fill the rest of the form with the following:

Rule name:



Method parameters should begin with a lowercase “p”


Method parameters should always begin with a “p”.


public void bringItHome( String pName, int pNumber, boolean pDoneThat, List pTr)

  1. Change rule priority to Error high
  2. Click on Add property… and enter xpath as name and //FormalParameter/VariableDeclaratorId[not (starts-with(@Image, 'p'))] as value.
  3. Click on OK and agree to rebuild.
  4. In Package Explorer right-click on QA Project and select PMD | Check Code with PMD. Open the PMD Violations view. It should display three ParameterNameConvention violations in

That’s it. The whole rule takes a single line:

//FormalParameter/VariableDeclaratorId[not (starts-with(@Image, 'p'))]

This one-line XPath rule tells PMD to watch for method parameters whose VariableDeclaratorId name (‘Image’) does not start with a “p”. Very elegant.

XPath-based PMD rules offer an efficient alternative to Java-based rules. As more development tools become XPath-compatible it is likely that an investment in this powerful query language will prove valuable for Java developers.


Thanks for looking into my blog.

The following two tabs change content below.

Chaman Shareef

Latest posts by Chaman Shareef (see all)

Submit a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current month ye@r day *