35

I have netbeans 7 and I was wondering how to calculate the total lines for a project. I have looked through Google but every time I do it I only find dead ends or non working plugins. Does anyone know how to count the lines?

1
  • I was able to successfully use wordcount with NetBeans 7.3 Oct 17, 2013 at 21:12

5 Answers 5

32

You can use wordcount that works with 7.1 nb-wordcount that works with 8.2.

To configure wordcount go in Tools->Options->Miscellaneous. You have to change Accept filename if you want other files than Java and Groovy to match.

To display the count window go in Window->Open WordCount Window.

To display stats click on WordCounting (second button). I will display the stats of the directory selected in Projects (window)(it has to be a package or something like Source Packages or Web pages, it won't work if you select the project).

Also if you are on linux you can simply execute :

 find . -name '*.java' | xargs wc -l
3
  • 1
    The wordcount plugin doesn't work in Netbeans 7.3.1, I can click at any scope and have always 0 files scanned.
    – s3m3n
    Aug 26, 2013 at 11:04
  • I just tested with NetBeans 8.0.2 and it works perfectly. Nov 2, 2015 at 13:08
  • 1
    Link to wordcount is broken ;) Jan 26, 2018 at 21:34
24

I know this is a very old question however there is a simpler way of finding the line count in a netbeans project that doesn't involve installing plugins:

  1. Right click on the folder or package you want to find the amount of lines in
    Note: Don't right click on the project itself as that will cause it to count the lines in all the generated files too.
  2. Click on Find or Find in Files or press CtrlF.
  3. Make sure the Match dropdown is set to Regular Expression.
  4. Type in \n into the search box.
  5. Press find and the amount of lines your project has will be displayed at the top of the
    Search Results tab.

Note: In NetBeans, the search is stopped after 5000 results, so if your project is longer than that then this method won't work

2
  • 1
    Heh. Why didn't I think of that! :-D
    – Simba
    Oct 30, 2015 at 14:23
  • 4
    It is limited to 5000 matches
    – Yevgen
    Nov 10, 2017 at 21:29
16

I was hoping for a cut-and-paste answer. So I wrote one.

EDIT: Supports millions of lines of code. No external libraries required.

public static void main(String[] args) throws FileNotFoundException {

    final String folderPath = "D:\\Dev\\MYPROJECT\\src";

    long totalLineCount = 0;
    final List<File> folderList = new LinkedList<>();
    folderList.add(new File(folderPath));
    while (!folderList.isEmpty()) {
        final File folder = folderList.remove(0);
        if (folder.isDirectory() && folder.exists()) {
            System.out.println("Scanning " + folder.getName());
            final File[] fileList = folder.listFiles();
            for (final File file : fileList) {
                if (file.isDirectory()) {
                    folderList.add(file);
                } else if (file.getName().endsWith(".java")
                        || file.getName().endsWith(".sql")) {
                    long lineCount = 0;
                    final Scanner scanner = new Scanner(file);
                    while (scanner.hasNextLine()) {
                        scanner.nextLine();
                        lineCount++;
                    }
                    totalLineCount += lineCount;
                    final String lineCountString;
                    if (lineCount > 99999) {
                        lineCountString = "" + lineCount;
                    } else {
                        final String temp = ("     " + lineCount);
                        lineCountString = temp.substring(temp.length() - 5);
                    }
                    System.out.println(lineCountString + " lines in " + file.getName());
                }
            }
        }
    }
    System.out.println("Scan Complete: " + totalLineCount + " lines total");
}

The results appear similar to the following:

   (truncated)
   47 lines in WarningLevel.java
Scanning design
 1367 lines in ProcessResultsFrame.java
   83 lines in TableSettingPanel.java
Scanning images
Scanning settingspanel
   67 lines in AbstractSettingPanel.java
  215 lines in AdvancedSettingsPanel.java
   84 lines in BaseSettingsPanel.java
  451 lines in DatabasePanel.java
  488 lines in EmailPanel.java
  458 lines in FTPGUIPanel.java
  482 lines in FTPScheduledTaskPanel.java
  229 lines in GUISettingPanel.java
   87 lines in RootSettingJPanel.java
  722 lines in ServerVisualIdentificationSettingPanel.java
Scan Complete: 123685 lines total

If it's missing something please let me know and I'll do my best to correct it. Thanks!

1
  • I know your answer is a bit old but I tried it and ran into an issue with charset on Windows 11 so I'll leave it here. I had to add , Charset.forName("Cp1252") in the Scanner constructor, which in turn made me use a try-with clause to handle the IOException that could have been raised. Doing so increased my line count from 4222 to 5332, which seems more reasonable (I had files with 0 lines).
    – Anab
    Jan 4 at 11:21
3

You could use Source Code Metrics for Java Projects.

6
  • 1
    Tried it but it never says anywhere hos many lines there are.
    – me me
    May 19, 2012 at 11:15
  • Try this : Windows > Source Code Metrics; then right click on your project and choose Source Code Metrics.
    – ltin
    May 19, 2012 at 11:21
  • It works for me using NetBeans IDE 7.1.2 on Mac OS X 10.7.4. Juste in case, here's exactly what I do and what I get while using this plugin. ffb777.data.s3.amazonaws.com/sof/…
    – ltin
    May 19, 2012 at 14:28
  • For anyone looking at this post with Netbeans 7.2 on Windows 7, It didn't work out of the box. Netbeans kept looking for older versions of jars and wouldn't except the newer versions. It killed my configuration and I had to manually remove plugin before I could get Netbeans to start again. Sep 12, 2013 at 22:48
  • Search for the LOC column (Lines of Code). Mar 27, 2014 at 13:59
0

@Johnathan, I liked your simple but useful LOC count program. While I was using it for my project I found that the Scanner will not always count the correct number of lines, sometimes it didn't even count a single line for a specific file. A little reseach led me to this article Java scanner not going through entire file. It seems that Scanner has some problems with different encodings. To avoid this and make the program more reliable I would suggest to replace the Scanner by a BufferedReader like so:

[...]
//  final Scanner scanner = new Scanner(file);
//  while (scanner.hasNextLine()) {
//      scanner.nextLine();
//      lineCount++;
//  }
InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (br.readLine() != null) {
    lineCount++;
}
[...]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.