Click here to Skip to main content
13,452,967 members (55,396 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 23 Dec 2010

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


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.


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 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;
String t=br.readLine();
t=t.replaceAll("\\n|\\t|\\s", "");
if((!t.equals("")) && (!t.startsWith("//"))) {
i = i + 1;
else {
return i;


  • 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.


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


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


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 nice Pin
d_lucifer8-Jul-11 3:28
memberd_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
memberAsit Banerjee18-Jan-11 21:16 
GeneralReason for my vote of 5 like Pin
rajaduraihycus30-Dec-10 2:19
memberrajaduraihycus30-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180318.3 | Last Updated 24 Dec 2010
Article Copyright 2010 by makpandian
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid