serving the solutions day and night

Pages

Sunday, February 27, 2011

Checkstyle 5.3

  1. CheckStyle is a tool to help programmers to write JAVA coding standard.
  2. Download and unzip checkstyle-5.3 into a convenient directory.
  3. It is a program to inspect your JAVA source code and list/point out errors that deviate from a defined set of JAVA coding standard.
  4. By default it supports the Sun JAVA Code Conventions (sun_checks.xml), and also support any customize coding standard.
  5. It can be invoked with an ANT task, a command line program and integrated into IDE (Eclipse, IntelliJ, NetBeans) and other tools (TextPad).
  6. Run CheckStyle - command line program with configuration file sun_checks.xml
    java -jar "C:\Program Files\checkstyle-5.3\checkstyle-5.3-all.jar" \ -c "C:\Program Files\checkstyle-5.3\sun_checks.xml" JavaFileName.java
  7. Configuring CheckStyle - Eclipse
    1. In Eclipse go to Help->Install New Software
    2. Enter Work With : Eclipse CheckStyle - http://eclipse-cs.sourceforge.net/update, then check CheckStyle checkbox, then Next button and finally accept terms and press the Finish button. Restart Eclipse.
    3. Right click the project Properties->select Checkstyle.
    4. Check CheckStyle active for this project checkbox. Then press OK button.
    5. After, some warnings from Checkstyle point/list to the code locations where your code not followed the Sun Checks configuration.
  8. Configuring CheckStyle - TextPad with configuration file sun_checks.xml
    1. In TextPad, click on the Configure menu and then Preferences, the Preferences dialog window appears.
    2. Click on the Tools node of the left tree, You will see a Tools list box and an Add drop-down menu.
    3. Click on the Add button and then Program. The Select a file dialog appears. Change the "Files of type:" setting to All Programs (*.*).
    4. Browse to where you installed CheckStyle, select the checkstyle-5.3-all.jar file and Press the Apply button. CheckStyle should now appear as a choice in the Tools menu.
    5. Expand the Tools menu and click on the CheckStyle tool.

      Change the Command line to: java

      Change the Parameters line to:
      -jar "C:\Program Files\checkstyle-5.3\checkstyle-5.3-all.jar" \ -c "C:\Program Files\checkstyle-5.3\sun_checks.xml" $FileName

      Check the Capture output checkbox.

      Set the Regular expression to match output to:
      ^\(\(\(.[^:]\)\|\([A-Za-z]:\)\)[^:]+\):\([0-9]+\):

      Set the Registers:File: 1, Line: 4, Column: (blank)

      Press the OK button.
    6. Run a sample commented/non-commented HelloWorld.java java program to verify it works correctly.
      /**
      * The HelloWorld class for testing CheckStyle + TextPad.
      *
      * @author Daynight
      * @version 1.0 03/04/11
      */
      public class HelloWorld {
      /**
      * The main method for the application.
      *
      * @param args Not used.
      */
      public static void main(String[] args) {
      System.out.println("Hello, world!");
      }
      }
    7. To test the CheckStyle configuration: From the Tools menu->External Tools->choose CheckStyle
That's it.

No comments: