Counting Line Numbers in Eclipse [closed]
Asked Answered
C

9

149

I have a Java project in Eclipse with ~10 packages and ~10 class files per package. Is there a way to determine total lines of code for the whole project from within Eclipse? I am familiar with other tools (e.g., Code Analyzer, wc, etc.) but I want to know if there is a way to do this within Eclipse (or get confirmation that there is no way to do it).

Clearing answered 25/6, 2009 at 12:38 Comment(0)
A
98

Here's a good metrics plugin that displays number of lines of code and much more:

http://metrics.sourceforge.net/

It says it requires Eclipse 3.1, although I imagine they mean 3.1+

Here's another metrics plugin that's been tested on Ganymede:

http://eclipse-metrics.sourceforge.net

Admixture answered 25/6, 2009 at 12:41 Comment(4)
Note that metrics2.sourceforge.net states that the first plugin above (metrics.sourceforge.net) is discontinued and metric2 is intended to be a continuation.Cantonment
An information that would saved me a ton of time: The plugin needs to be activated for each project in your workspace individually, will modify your .project file and only provides metrics up to the project level, but not the workspace level.Homology
both link seems deadBernadine
On Eclipse Mars 1, installing Metrics from the Help/Eclipse Marketplace failed - Metrics did not show up in Window/Show View/Other. Instead installing by adding it from metrics.sourceforge.net/update as described at metrics.sourceforge.net workedHag
D
179

Search > File Search

Check the Regular expression box.

Use this expression:

\n[\s]*

Select whatever file types (*.java, *.xml, etc..) and working sets are appropriate for you.

Dervish answered 13/7, 2011 at 4:4 Comment(9)
... and by check off you mean, make sure it is checked. Note that this method does not count empty lines.Isochronize
You may or may not have got this from here.Jaggers
This will use up a lot of memory on larger codebases, unfortunately. On my Indigo I hit 700k hits before eclipse died. Granted I only have -Xmx314M in my eclipse.iniRedolent
This gives each file's LOC individually. not TOCAsiatic
As mentioned by @PeterAjtai, it does not count empty lines. However, this will: \n[^\n]*Feedback
@AbhishekSusarla You can run it on packages.Romish
@CamJackson you're link is dead; anyway i definitely got it from somewhere, just can't remember else i'd have just posted the link...Dervish
To return a count of non-blank, non-comment lines something like this could help. \n[^!//][\s]*Hop
This search usually hangs Eclipse (4.2.1) on my moderate size project.Booster
A
98

Here's a good metrics plugin that displays number of lines of code and much more:

http://metrics.sourceforge.net/

It says it requires Eclipse 3.1, although I imagine they mean 3.1+

Here's another metrics plugin that's been tested on Ganymede:

http://eclipse-metrics.sourceforge.net

Admixture answered 25/6, 2009 at 12:41 Comment(4)
Note that metrics2.sourceforge.net states that the first plugin above (metrics.sourceforge.net) is discontinued and metric2 is intended to be a continuation.Cantonment
An information that would saved me a ton of time: The plugin needs to be activated for each project in your workspace individually, will modify your .project file and only provides metrics up to the project level, but not the workspace level.Homology
both link seems deadBernadine
On Eclipse Mars 1, installing Metrics from the Help/Eclipse Marketplace failed - Metrics did not show up in Window/Show View/Other. Instead installing by adding it from metrics.sourceforge.net/update as described at metrics.sourceforge.net workedHag
M
18

Under linux, the simpler is:

  1. go to the root folder of your project
  2. use find to do a recursive search of *.java files
  3. use wc -l to count lines:

To resume, just do:

find . -name '*.java' | xargs wc -l    
Melson answered 20/2, 2015 at 1:4 Comment(3)
Guess, simpler depends on the defintion here.Proofread
And people are installing an eclipse plugin to load their memory. As if it was already not loaded too much.Serra
Worked well for me too.Vitrain
F
7

For eclipse(Indigo), install (codepro).

After installation: - Right click on your project - Choose codepro tools --> compute metrics - And you will get your answer in a Metrics tab as Number of Lines.

Fingered answered 4/10, 2013 at 9:19 Comment(2)
This one is actually quite good! You should however fix the link as it gives a 404.Kovach
Found this post that has a link to the latest version of codepro - and it works in Eclipse Oxygen: #29390808Trudietrudnak
C
6

Are you interested in counting the executable lines rather than the total file line count? If so you could try a code coverage tool such as EclEmma. As a side effect of the code coverage stats you get stats on the number of executable lines and blocks (and methods and classes). These are rolled up from the method level upwards, so you can see line counts for the packages, source roots and projects as well.

Crespo answered 25/6, 2009 at 12:57 Comment(0)
C
4

You could use a batch file with the following script:

@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir "%CD%\src\*.java" /b /s') DO (type "%%G") >> lines.txt
SET count=1
FOR /f "tokens=*" %%G IN ('type lines.txt') DO (set /a lines+=1)
echo Your Project has currently totaled %lines% lines of code. 
del lines.txt
PAUSE
Collagen answered 23/7, 2015 at 3:32 Comment(0)
A
2

I think if you have MyEclipse, it adds a label to the Project Properties page which contains the total number of source code lines. Might not help you as MyEclipse is not free though.

Unfortunately, that wasn't enough in my case so I wrote a source analyzer to gather statistics not gathered by other solutions (for example the metrics mentioned by AlbertoPL).

Adhere answered 25/6, 2009 at 12:45 Comment(2)
Care to share it for community's use?Hankypanky
@Gala101: code.google.com/p/javasourcestatAdhere
D
2

A very simple plugin for counting actual lines of source code is step counter eclipse plugin. Please download and try.

github link

Place the downloaded jar file under eclipse\plugin folder and restart eclipse.

Rightclick and select step counter enter image description here

Step Result enter image description here

Dionysian answered 30/3, 2017 at 2:15 Comment(0)
H
1

You could use former Instantiations product CodePro AnalytiX. This eclipse plugin provides you suchlike statistics in code metrics view. This is provided by Google free of charge.

Hirz answered 10/9, 2013 at 11:20 Comment(1)
This is the same hint as Ashish gave, but with a working link.Kovach

© 2022 - 2024 — McMap. All rights reserved.