Menu

Home

John Dalbey

Java Lines of Code Counter

This application computes the size of a Java source code file by counting the "logical lines of code." The application is written in Java and has been tested under JRE 1.5 on Linux and Windows 7.

Download

Looking for the latest version? Download JavaLOC.jar (44.9 kB)

Instructions for Use

If jar file associations were setup when you installed Java, you can double-click on the JavaLOC.jar file to launch it. Otherwise, from the command line issue:

java -jar JavaLOC.jar

Screenshot

Click the "Choose File" button to open a standard file chooser dialog and then navigate to the desired file. When you select a file, the filename and the line count will appear in the display window.

In the file chooser, you may select multiple files by Ctrl-clicking. The total count appears at the bottom.

Remove the check mark if you don't want a file included in the total.

Command Line Usage

Count lines in a single file:

java -cp JavaLOC.jar textui.LOC filename

Count lines for all files in a given directory:

java -cp JavaLOC.jar textui.LOC -d directory

Count lines for all files in the current directory:

java -cp JavaLOC.jar textui.LOC -d .

Count lines for all files in a directory and recurses into subdirectories (ignoring those starting with '.'):

java -cp JavaLOC.jar textui.LOC -r -d directory

Count lines for all files on the command line:

java -cp JavaLOC.jar textui.LOC file1 file2

Count lines for all files on the command line. path will be prefixed to every filename:

java -cp JavaLOC.jar textui.LOC -p path file1 file2

Count lines for filenames read from stdin:

java -cp JavaLOC.jar textui.LOC

Count lines for filenames read from stdin. path will be prefixed to every filename:

java -cp JavaLOC.jar textui.LOC -p path

Counting rules

The number of lines is computed as the number of terminal semicolons and right braces.

(Semicolons within a for statement are not counted).
Comment statements are ignored.

For example, the following code has a line count of 6.

    void Snoopy()
    {
        int x = 0, y = 1;
        x = x + 1; y = y + 3;
        if (x == 10)
        {
            y = 0;
        }
    }    

There are four semicolons and two right braces, resulting in 6 lines.

Note that the program does not actually parse the Java code, it does a simple text search to find semicolons and braces. Consequently the file does not have to be valid Java code in order to produce a count. The tool will produce a count for C code, psuedocode, or any plain text file.

Icon

Here's an icon you can use to associate with the application shortcut.

Ant target

Here's a sample Ant target for use in your build scripts. You'll need to modify the classpath and arg line to suit your needs.

    <target description="Count lines of code." name="loc">
        <java classname="textui.LOC"
            classpath="lib/JavaLOC.jar"
            fork="true">
                <arg line="-d src -r"/>
        </java>
    </target>

Creative Commons License

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Join us

Want to contribute to our project? Send an email to the project administrator (below) requesting to be involved. We'll upgrade your member status as appropriate. Thanks!

Project Members: