Tuesday 13 March 2018

A short tutorial on FindBugs

In this tutorial series, you are going to learn
a.   What is FindBugs
b.   System Requirments
c.   Platform independency of FindBugs
d.   Install FindBugs
e.   Running FindBugs
f.    Running the Analysis using FindBugs GUI
g.   Saving and Opening the results
h.   How to install FindBugs in Eclipse
i.     Configure FireBug in Eclipse
j.    How to find the bugs using FindBugs in Eclipse

What is FindBugs?
FindBugs is a tool that uses static analysis to find bugs in Java code. At the time of writing this article, the current version of FindBugs is 3.0.1.

System Requirements
FindBugs requires JRE (or JDK) 1.7.0 or later to run.

Is FindBugs platform Independent?
Yes, it can run on Windows, Linux MacOS X platforms.

Install FindBugs
Download FindBugs executable from below location.

Extract the downloaded zip file.

The extracted zip contains below structure.


Set the 'FINDBUGS_HOME' property to the installed directory location of FindBug. In my case, I extracted the downloaded zip file to below location.

C:\Users\krishna\softwares\findbugs\findbugs-3.0.1.

Running FindBugs
If you are running on Linux systems, run below command.
java -jar $FINDBUGS_HOME/lib/findbugs.jar

If you are running on Windows systems, run below command.
java -jar %FINDBUGS_HOME%\lib\findbugs.jar

On you ran above command, it opens user interface like below.

I set the value of FINDBUGS_HOME' to ‘C:\Users\krishna\softwares\findbugs\findbugs-3.0.1’.

Running the Analysis using FindBugs GUI
Go to File -> New Project.

It opens below kind of window.
Give the project name as ‘My_Findbugs_demo’, click on the Add button to add jar files to analyze. Add the sources using the third pane of the window.

Click on the button ‘Analyze’. FindBugs starts analyzing process.
When the analysis completes, you will see a screen like the following.

Saving and Opening the results
You may use the File Save as... menu option to save your work. To save your work, including the jar file lists you specified and all bug results, choose "FindBugs analysis results (.xml)" from the drop-down list in the "Save as..." dialog. There are also options for saving just the jar file lists ("FindBugs project file (.fbp)") or just the results ("FindBugs analysis file (.fba)"). A saved file may be loaded with the File Open... menu option.

How to install FindBugs in Eclipse?
Open Eclipse.

Help -> Install New Software.
Work with the below url.


Once the installation is done, restart the Eclipse.

Configure FireBug in Eclipse
Window->Preferences->Java->FindBugs

How to find the bugs using FindBugs in Eclipse
We need to create and build a java project to find bugs using FindBugs. Add some classes to the java project.

Now, Build the project using Project->Build Project.

Now Right click on the project -> Find Bugs -> Find Bugs.


Now you can see all the bugs in bug explorer. Window->Show View->Other->FindBugs->Bug Explorer.

You may like



No comments:

Post a Comment