Count lines of code and number of classes in eclipse projects

There are a number of plugins for eclipse, Maven and/or subversion, which allow the convenient and automatic determination of important project statistics such as the total lines of code in a project or the number of Java classes. However, sometimes one just needs these statistics quick and dirty and setting up sophisticated tools represents an unjustifiable overhead.

In this case, the eclipse search function is a useful tool to determine these statistics. Below are simple workflows of attaining the statistics for one or more projects.

Lines Of Code

The lines of code can be determined by searching all the Java source files in the selected resource for line breaks. The number of line breaks should approximate the lines of code.

1. Select the projects for which you want to count the lines of code in the Package Explorer.

2. Open through Menu Search / File

3. Search for the regular expression ‘\n’ in all ‘*.java’ files of the selected resources (these are the projects you selected in step 1 – be aware that the Package Explore must hold the current focus in order for this to work. If ‘selected resources’ is greyed out, reselect the projects in the package explorer in reopen the search dialog)

Note: If you would like to count only non-empty lines, you can use the expression: ‘\s+\n’ instead of ‘\n’ (Thanks, Anshul!)

4. Perform the search. The total matches reported in the Search tab at the bottom of your screen should approximate the lines of code in your Java sources. The matches reported for individual Java files, should represent the lines of codes for these files (actually the lines in the file -1 as pointed out by shahnaz below 🙂 ).

Number of Classes

The number of classes can be determined using the eclipse Java search for all declared types.

1. Select the projects for which you want determine the number of classes as shown above.

2. Select Menu / Search

3. Select the tab Java Search and search for all declared types as depicted below

4. Perform the search. The total number of declarations reported should represent the total number of classes and interface in the projects you have selected.

shahnaz

11 thoughts on “Count lines of code and number of classes in eclipse projects

  1. Well 1 issue..it ommits the last line…so I will always get 1 line less for each file..imagine if for the large project having many files…

    1. oh! Good idea. I don’t think this can be easily done (although there might be a regex wizard, who can come up with a matching expression? – I admit I tried to find it but as usual failed).

      You could try a tool like Sonar, which will not only exclude empty lines but also imports etc.

    2. Yes, thanks I found it very useful too. To count only non-empty lines, use:

      \s+\n

      (the \s denotes a non-white space character, while the + indicates at least one such non-white space character, followed by a newline character)

      1. Thanks, that is great! I’ve added a note in the post with your regex for people like me, who design regular expressions best by copy & paste 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s