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.


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

  1. Well 1 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:


      (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 😉

  2. Can we extract the output.
    Like say,when it is showing the number of lines,can I use that number in my class?

    1. Thank you for your comment!

      Could you clarify what you would like to achieve? Do you want to get access to the number of lines of code of a class from a running Java app?

  3. Hi Max…I need to get the number of lines of code from all the classes of the package(Excluding comment lines and blank lines).
    I know I could write a code for this.But I was wondering after seeing your blog,whether I can take that value directly from eclipse somehow:)

    1. Thank you for clarifying 🙂

      Yes, there is indeed a way to do this. Instead of the step:

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

      Select the packages for which you want to get the lines of code.

      Let me know if it works!

      1. Hi Max,

        Thanks for your reply.
        I am able to achieve this.
        However I want to take this value(Number of lines) from the console and use it in one of my other java class.
        Is this possible?

      2. Hi Prasuna, you mean to access the value in your Java source code like getLinesOfCode(classname)?

        That is unfortunately not possible. Java programs are compiled into bytecode and so when you run them, they ‘forget’ what source code they were compiled from!

      3. Thanks Max.
        I knew it wasn’t possible but didn’t know why.So, thanks for sharing the reason:)

Leave a Reply

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

You are commenting using your 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