Picture

Hi, I'm Stephen Nicholas.

And this is my blog. I'm a software engineer by trade (but mostly for fun) with a wide variety of experience and interests. Some of which I'll write about here.

Eclipse quick line count

Today I needed to find out how many lines of code were in one of my projects, so I loaded up Eclipse, loaded the project and then… was stumped. I couldn’t think what to do next. Surely, I thought, there must be a simple menu option for that, but no I couldn’t find anything and it appears that Eclipse doesn’t have this functionality built-in. Colour me boggled.

Anyway, a quick search pointed me to a number of plug-ins I could install, but also to a really simple ‘hack’ (which I found  here). I thought it was so nice, I’d share it here in its entirety for everyone to see.

The steps are as follows:

  1. Open the Eclipse search dialog. ‘Search -> File…’ from the main menu.
  2. Set the ‘Containing text:’ to ‘\n’.
  3. Check the ‘Regular expression’ checkbox.
  4. Set the ‘File name patterns:’ to ‘*.java’.
  5. Select a scope that is appropriate. The whole workspace, the current project or a working set.

Eclipse search dialog

Then when you run the search, it will come back with a search result for every line of code in your application. You can see the total number of matches at the top, and it’s also broken down by class; exactly what I wanted. Here’s some example results from another project:

Eclipse search results

Now, there does appear to be one issue with this approach, as it can sometimes not count the final line in a class (if it doesn’t have a line break after it), but in my case that only left me about 30 lines out and that was good enough for me. Cool eh?

Additional

Here’s a couple of other regexs that I found useful for reducing the above results to just lines of code:

//.*(\n{1})? – search for comment lines.

^\s*\n – search for blank lines.