370

How to count lines of Java code using IntelliJ IDEA?

1

9 Answers 9

521

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

15
  • 20
    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
    Commented Jul 19, 2013 at 17:41
  • 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
    Commented Feb 26, 2014 at 13:08
  • 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
    Commented Nov 9, 2017 at 12:16
  • 1
    This plugin doesn't work at all in 2018.1. The View -> Tool windows -> Stastistic just shows an empty window. Commented Sep 5, 2018 at 6:58
  • 5
    @YngvarKristiansen You have to do an initial refresh (top left corner) Commented Nov 15, 2018 at 17:19
124

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.

9
  • 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
    Commented Jul 10, 2012 at 14:41
  • 1
    Neat idea but only if you either trim all empty lines first or explicitly want them to be counted. Commented Sep 5, 2014 at 19:34
  • 3
    @MarcelStör Try it with "(.+)\n"
    – algorhythm
    Commented Sep 19, 2014 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
    Commented Jan 20, 2015 at 17:01
  • 5
    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
    Commented Nov 2, 2019 at 15:29
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...

9
  • Agreed, documentation is needed. How does one activate the plugin once it is installed? Commented Mar 29, 2012 at 17:24
  • 4
    To answer my own question: Analyize -> Calculate Metrics Commented Jul 26, 2012 at 18:18
  • Doesn't work with IntelliJ IDEA 12: github.com/BasLeijdekkers/MetricsReloaded/issues/19
    – RenniePet
    Commented Jul 19, 2013 at 17:14
  • Works in IDEA 12 now, not found in the repository you need to install via manually downloading. Commented Sep 18, 2013 at 15:36
  • 1
    As of 2021.2, it works just fine. Commented Sep 30, 2021 at 21:22
45

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
4
  • 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
    Commented Oct 10, 2013 at 16:23
  • 2
    This will return too many lines. Empty lines should not be counted Commented Dec 19, 2016 at 16:40
  • 5
    ignore blank lines: find . -type f -name '*.java' | xargs cat | grep -ve '^\s*$' | wc -l
    – SimpleSam5
    Commented May 21, 2019 at 15:51
  • this also works in Windows with 'git bash' installed Commented Nov 3, 2024 at 23:03
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.

1
  • 2
    This is really slow, use only if there's no better way. I recommend installing GitBash and using the find. Commented Mar 11, 2019 at 13:17
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

5

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

1
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$
2

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.