Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Java
Tip/Trick

Simple Source Line Counter in Java for Java

Rate me:
Please Sign up or sign in to vote.
4.50/5 (7 votes)
24 Dec 2010CPOL2 min read 60.7K   2   3
A simple source code line counter written in Java

Introduction

Did you ever think about how many lines of code you have written in your Java application? Do you need to know how many lines of code there are in Java Development Kit (JDK) or some Java open sources. If yes, you will certainly like it very much.

Well, SLOC Counter is a simple Java Swing application that counts the lines of code in a Java Source file. In other words, it's a Windows desktop utility that is used for quantitative analysis of Java application. Note that I did not care about quality analysis in this article.

Background

I would like to take this opportunity to thank Tom Ollar who wrote the article "Simple Source Line Counter in C# for C#" on CodeProject. I had read that article while I was in college. It was the only thing that inspired me to write this article. Thanks again to Tom Ollar.

How to Use?

Unzip SLOC Counter Demo.zip and run the executable SLOC Counter.jar. Then click the "Browse" button to select the folder where the root package of your Java application resides and click "Submit" button. Remember that all sub packages in your root package will be considered for counting purposes. That's it. I have used Java development kit source for demo purposes.

Now, SLOC Counter lets you know the following information about your Java application:

  • Total lines of code in your Java class, package and application
  • Total number of package in your application
  • Total number of classes in your application

Using the Code

The main logic that counts the line of code in a single Java class file is shown as below:

Java
private static int getLines(File f) throws IOException { 
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr); 
int i=0;
boolean isEOF=false;
do{
String t=br.readLine();
if(t!=null){
isEOF=true;
t=t.replaceAll("\\n|\\t|\\s", "");
if((!t.equals("")) && (!t.startsWith("//"))) {
i = i + 1;
}
}
else {
isEOF=false;
}
}while(isEOF);
br.close();
fr.close();
return i;
}

Advantages

  • If your package contains some text files which are not .java extension, it will not be counted for process.
  • Empty line and single line comment will not be counted.

Disadvantages

  • We could use this application in Windows platform only.
  • Multiline comment will be counted is the big disadvantage of this tool.

Conclusion

As for me, it is a very simple tool to count the source lines for Java application. Hope you like this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer RoyalSoft Services Ltd
India India
I am a Software Engineer working in Java Web Platform.

Comments and Discussions

 
GeneralReason for my vote of 5 nice Pin
d_lucifer8-Jul-11 3:28
d_lucifer8-Jul-11 3:28 
GeneralReason for my vote of 4 It's a cool tool for line counting a... Pin
Asit Banerjee18-Jan-11 21:16
Asit Banerjee18-Jan-11 21:16 
GeneralReason for my vote of 5 like Pin
rajaduraihycus30-Dec-10 2:19
rajaduraihycus30-Dec-10 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.