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?
5 Answers
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
-
1The
wordcount
plugin doesn't work in Netbeans 7.3.1, I can click at any scope and have always 0 files scanned.– s3m3nAug 26, 2013 at 11:04 -
-
1
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:
- 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. - Click on
Find
orFind in Files
or press CtrlF. - Make sure the
Match
dropdown is set toRegular Expression
. - Type in
\n
into the search box. - 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
-
1
-
4
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!
-
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).– AnabJan 4 at 11:21
You could use Source Code Metrics for Java Projects.
-
1
-
Try this : Windows > Source Code Metrics; then right click on your project and choose Source Code Metrics.– ltinMay 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/…– ltinMay 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
-
@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++;
}
[...]