Skip to main content
This revision made May 18, 2011 23:06, 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 "logical lines of code" in the file. The application is written in Java and has been tested under JRE 1.3 on Windows.

Creative Commons License

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

Download

LOC.jar Version 2.1 5-20-2010

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

3) Count lines in a single file:

 java -cp LOC.jar textui.LOC sourcefile.java

4) Don't count comments:

 java -cp LOC.jar textui.LOC -n sourcefile.java

5) Count lines in a file in specified location

 (Windows) java -cp LOC.jar textui.LOC  C:\somedir\sourcefile.java
 (Unix)  java -cp LOC.jar textui.LOC   /home/user/src/sourcefile.java

6) Count lines in multiple files from stdin

 (Windows) java -cp LOC.jar textui.LOC
 (Unix) java -cp LOC.jar textui.LOC

7) Count lines in multiple files in specified directory.

 (Windows) java -cp LOC.jar textui.LOC -d C:\myproject\
 (Unix) java -cp LOC.jar textui.LOC -d /home/user/Docs
 

Counting rules

8) 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:
 */
 

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: LOCtestcase1.txt

Icon

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

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> === Test Data File === Here is the file we used as test data to verify the software: [http://users.csc.calpoly.edu/~jdalbey/SWE/PSP/LOCtestcase1.txt LOCtestcase1.txt]<span style="text-decoration:underline;color:green">=== Icon === Here's an [http://tinypic.com/r/103xs9i/7 icon] you can use to associate with the application shortcut. </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