Skip to main content
This revision made April 10, 2012 01:23, by john_roshi

Lines of Code Counter

Purpose

The purpose of this application is to compute the size of a source code file by counting the "physical lines of code" in the file subject to some rules for omitting comments and blank lines. The application is written in Java and has been tested under JRE 1.5 on Linux.

Creative Commons License

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

Download

LOC.jar Version 3.0 4/7/2012

Instructions for Use

If jar file associations were setup when you installed Sun's JVM, you can double-click on the LOC.jar file to launch it. Otherwise, from the DOS prompt issue:

    java -jar LOC.jar

1) 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.

2) Uncheck the check box if you don't want comments included in your line count.

Limited drag-and-drop support. If the application has the focus, you can drag files from elsewhere and drop them into the application window.

Command Line Usage

Single File LOC count: 
  java -cp LOC.jar textui.LOC <file>
       Show counts for one file on the command line


Directory LOC count: 
  java -cp LOC.jar textui.LOC -d <directory>
       Show counts for all files in a directory
  java -cp LOC.jar textui.LOC -d .
       Show counts for all files in the current directory
  java -cp LOC.jar textui.LOC -r -d <directory>
       Show counts for all files in a directory
       and recurses into subdirectories (ignoring those starting with '.')
 
Multiple File LOC count: 
  java -cp LOC.jar textui.LOC 
       Show counts for filenames read from stdin
  java -cp LOC.jar textui.LOC <file1> <file2>
       Show counts for all files on the command line
  java -cp LOC.jar textui.LOC -p <path>
       <path> will be prefixed to every filename read from stdin
  java -cp LOC.jar textui.LOC -p <path> <file1> <file2>
       <path> will be prefixed to every filename on the command line
 

 

Counting rules

The rule is simple: we count any line that has more than 3 non-blank characters.

This rule means that a single brace or a comment line containing only double slashes won't count.

This rule places some responsibility on the programmer to format his or her code in a sensible fashion. If you do silly things like this

 public static
    void main
      (Strings args[])

each line will be counted, which probably is cheating. The best way to manage this is to follow an established coding standard.

If you elect to NOT count comments, your source code needs to follow certain conventions to be counted properly:

 // Lines that start with two slashes or
 -- two dashes are considered comments
 /* C-style comments can end on the same line like this */
 /* A "block" comment may continue across
 lines like this, as long as you started the first
 line with the slash-star */
 But if you start the line with code you 
 can't continue across lines /* by starting
                                at the end of a line
                                like this */
 Because you should // use trailing comments
                   // like this
                   // instead
 /* Similarly, don't put code
 you want counted */ after a close block comment like this
 We can't deal with nested block comments -- the first star-slash
 will terminate the block (as do most compilers).
 
 /*
 Since there must be 4 or more non-blank characters on the
 line to be counted, the above
 line won't count (even if you are counting comments).
 Nor will the following:

		x

		xx

		{}

		1

		a;

 */
 

Known Anomalies

The HTML character &nbsp is treated like a character, not a blank. This can cause miscounts if you created your source by cutting and pasting from an HTML editor.

Test Data File

Here is the file we used as test data to verify the software: LOCtestdata.txt

The result is 19 lines including comments, or 7 lines without.

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/LOC.jar"
            fork="true">
                <arg line="-d src -r"/>
        </java>
    </target>

Join us

Want to contribute to our project? First, create an account on Java.net. Then return to this page and bookmark this project. That will make you an "Observer" of the project. Then send an email to the project administrator (john_roshi at java dot net) requesting to be involved. We'll upgrade your member status as appropriate. Thanks!

Difference compared to previous revision
<span> Since there must be 4 or more non-blank characters on the line to be counted, the above line won't count (even if you are counting comments). Nor will the following:<span style="text-decoration:underline;color:green"><pre></span> x xx ... 1 a;<span style="text-decoration:underline;color:green"></pre></span> */ === Known Anomalies === ... The result is 19 lines including comments, or 7 lines without. === Icon === Here's an [http://tinypic.com/r/103xs9i/7 icon] you can use to associate with the application shortcut.<span style="text-decoration:underline;color:green"> === 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. <pre> <target description="Count lines of code." name="loc"> <java classname="textui.LOC" classpath="lib/LOC.jar" fork="true"> <arg line="-d src -r"/> </java> </target> </pre></span> === Join us === Want to contribute to our project? First, [https://java.net/people/new create an account] on Java.net. Then return to this page and [http://java.net/projects/loc-counter/watch bookmark this project]. That will make you an "Observer" of the project. Then send an email to the project administrator (john_roshi at java dot net) requesting to be involved. We'll upgrade your member status as appropriate. Thanks! <span>
 
 
Close
loading
Please Confirm
Close