352

How to count lines of Java code using IntelliJ IDEA?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Gary
  • 6,357
  • 5
  • 30
  • 36
  • For repository statistics, you can use [Gitinspector](https://github.com/ejwa/gitinspector). Source: https://stackoverflow.com/q/1828874/873282 – koppor Mar 01 '18 at 07:12

9 Answers9

491

The Statistic plugin worked for me.

To install it from Intellij:

File - Settings - Plugins - Browse repositories... Find it on the list and double-click on it.

Access the 'statistic' toolbar via tabs in bottom left of project screen capture of statistic toolbar, bottom left

OLDER VERSIONS: Open statistics window from:

View -> Tool Windows -> Statistic

larham1
  • 11,736
  • 5
  • 35
  • 26
  • 18
    The Statistic plugin works with IntelliJ IDEA 12, and even shows counts and percentages for comment lines and blank lines vs. total lines. Could use a bit more documentation. Launch it via new button that is installed in lower left corner of IntelliJ IDEA window. It has additional settings: File - Settings - (Project Settings) Statistic. – RenniePet Jul 19 '13 at 17:41
  • This still works in IntelliJ 13, just ran it on the EAP version. – Daegalus Aug 06 '13 at 00:07
  • 1
    The Statistic plugin provides file counts in addition to line counts. I was unable to find file counts with the Metrics Reloaded plug-in. – Jade Feb 26 '14 at 13:08
  • Works fine with Android Studio 0.8.x – Chris Nevill Sep 04 '14 at 13:45
  • Works fine with IntelliJ IDEA 14 CE. Thanks for this brilliant plugin. – Sri Harsha Chilakapati Jan 17 '15 at 10:28
  • Well it is a bit annoying I'd like to remove `test` folders, but there's no directory wildcard to exclude – bric3 Nov 26 '15 at 10:41
  • Unfortunately, IDEA hangs on “Calculating Statistics...”. – Sieva Kimajeŭ Nov 23 '16 at 10:13
  • For me it's not working (anymore) on bigger projects (> 15.000 lines of code). http://stackoverflow.com/questions/40707683/intellij-idea-statistic-plugin-out-of-memory – BullyWiiPlaza Nov 28 '16 at 10:38
  • 4
    Works on IDEA 2017.2.5, project > 150 kLOC, but does not group counts, no per module / per source folder sums --> mostly useless :-/ – barfuin Nov 09 '17 at 12:16
  • 1
    This plugin doesn't work at all in 2018.1. The View -> Tool windows -> Stastistic just shows an empty window. – Yngvar Kristiansen Sep 05 '18 at 06:58
  • 5
    @YngvarKristiansen You have to do an initial refresh (top left corner) – Alexandru Tomuta Nov 15 '18 at 17:19
  • 1
    it works with the latest phpstorm (I'm using it only for JS though) in Dec 2018 – Wang Sheng Dec 12 '18 at 02:43
  • @barfuin, "User can select (Project/Module/Package/File) scope using the 'Refresh on selection' button" – Vadzim Jun 05 '20 at 13:31
  • I downloaded the plugin and restarted IntelliJ. No menu option was added under View -> Tool Windows, and while the plugin is installed the Find Action doesn't see anything under "Statistic" other than the plugin itself. I don't think it's compatible with IntelliJ 18.3. – Gary Rush Jan 07 '21 at 22:04
  • @garyrush Thanks for feedback. I updated article to indicate new location (using Idea 2020.3), accessed via bottom-left toolbars. See screenshot. – larham1 Jan 09 '21 at 00:10
121

Quick and dirty way is to do a global search for '\n'. You can filter it any way you like on file extensions etc.

Ctrl-Shift-F -> Text to find = '\n' -> Find.

Edit: And 'regular expression' has to be checked.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
Neil
  • 1,821
  • 4
  • 14
  • 27
  • 3
    Yes, but this also searches through all the files that are in your libraries (ie: if you are creating a web application it looks through all the lines of JQuery for example) – somid3 Jul 10 '12 at 14:41
  • 1
    Neat idea but only if you either trim all empty lines first or explicitly want them to be counted. – Marcel Stör Sep 05 '14 at 19:34
  • +1 Nice solution. Very simply. I like simply solutions like this. – algorhythm Sep 19 '14 at 14:56
  • 3
    @MarcelStör Try it with "(.+)\n" – algorhythm Sep 19 '14 at 14:58
  • 3
    Searching for regex ".+" works better. It wont miss the last line in the file if it's missing a trailing newline like "\n" or ".+\n" will. It also semantically matches what your searching for better, that is, lines with characters. – Buttons840 Jan 20 '15 at 17:01
  • Intellij hung after finding 40k lines : / – B T Jun 17 '15 at 01:27
  • I ended up doing a plain old search for \n, which completed without hanging (the (.+)\n regex caused hanging). – B T Jun 17 '15 at 19:53
  • 3
    I cannot see the number of lines. It says "100+ matches in 3+ files" which isn't very helpful. I use IntelliJ IDEA Community 2019.2 – Qbyte Nov 02 '19 at 15:29
  • After doing the find, it only shows the number of files. Click on "Open in Find Window" and expand the "Found occurrences.." to see the number of lines. – David Gomes Nov 12 '22 at 19:37
55

In the past I have used the excellently named MetricsReloaded plugin to get this information.

You can install it from the JetBrains repository.

Once installed, access via: Analyze -> Calculate Metrics...

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
37

Although it is not an IntelliJ option, you could use a simple Bash command (if your operating system is Linux/Unix). Go to your source directory and type:

find . -type f -name '*.java' | xargs cat | wc -l
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
s.froehlich
  • 863
  • 1
  • 11
  • 19
  • 1
    I would do `find . -type f -name '*.java' | xargs cat | wc -l` (quoting the `*.java` part). Otherwise, the shell environment might expand the `*.java` too early and the command won't work properly. – cdmckay Oct 10 '13 at 16:23
  • 2
    This will return too many lines. Empty lines should not be counted – BullyWiiPlaza Dec 19 '16 at 16:40
  • 4
    ignore blank lines: `find . -type f -name '*.java' | xargs cat | grep -ve '^\s*$' | wc -l` – SimpleSam5 May 21 '19 at 15:51
25

Just like Neil said:

Ctrl-Shift-F -> Text to find = '\n' -> Find.

With only one improvement, if you enter "\n+", you can search for non-empty lines

If lines with only whitespace can be considered empty too, then you can use the regex "(\s*\n\s*)+" to not count them.

ItsJ0el
  • 55
  • 1
  • 5
TheRusskiy
  • 1,031
  • 12
  • 13
14

Statistic plugins works fine!

Here is a quick case:

  1. Ctrl+Shift+A and serach for "Statistic" to open the panel.
  2. You will see panel as the screenshot and then click Refresh for whole project or select your project or file and Refresh on selection for only selection.

statistic

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
5

now 2 versions of metricsreloaded available. One supported on v9 and v10 isavailable here http://plugins.intellij.net/plugin/?idea&id=93

Jenga Blocks
  • 121
  • 1
  • 2
  • 9
3

You can to use Count Lines of Code (CLOC)

On Settings -> External Tools add a new tool

  • Name: Count Lines of Code
  • Group: Statistics
  • Program: path/to/cloc
  • Parameters: $ProjectFileDir$ or $FileParentDir$
AA.
  • 4,496
  • 31
  • 35
1

To find all including empty lines of code try @Neil's solution:

Open Find in Path (Ctrl+Shift+F)

Search for the following regular expression: \n'

For lines with at least one character use following expression:

(.+)\n

For lines with at least one word character or digit use following expression:

`(.*)([\w\d]+)(.*)\n`

Notice: But the last line of file is just counted if you have a line break after it.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
algorhythm
  • 8,530
  • 3
  • 35
  • 47