Click here to Skip to main content
Click here to Skip to main content

Simple Source Line Counter in Java for Java

, 24 Dec 2010
Rate this:
Please Sign up or sign in to vote.
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:

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)

About the Author

makpandian
Web Developer RoyalSoft Services Ltd
India India
I am a Software Engineer working in Java Web Platform.
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 4 It's a cool tool for line counting a... PinmemberAsit Banerjee18-Jan-11 21:16 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 24 Dec 2010
Article Copyright 2010 by makpandian
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid