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
3 Comments
wordcount plugin doesn't work in Netbeans 7.3.1, I can click at any scope and have always 0 files scanned.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
FindorFind in Filesor press CtrlF. - Make sure the
Matchdropdown is set toRegular Expression. - Type in
\ninto the search box. - Press find and the amount of lines your project has will be displayed at the top of the
Search Resultstab.
Note: In NetBeans, the search is stopped after 5000 results, so if your project is longer than that then this method won't work
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 Comment
, 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).You could use Source Code Metrics for Java Projects.
6 Comments
@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++;
}
[...]