Sunday, 23 February 2014

JTail - Java Tail command with NIO 2

The old way

As far as I can tell every implementation of the UNIX tail command in java uses a variation of the following2 3:
open file
while (true)
        read file
        write file to output
        thread.sleep(1 second)
    } catch (InterruptedException) {}
close file

Now if you look at the source code of tail5 in UNIX, it checks for the existence of inotify1. Java 7 has something similar (but with less features) called the WatchService as part of the NIO.2.

The new way with NIO 2

The part containing the code interfacing with the WatchService of NIO.2 can be found in the FileSystemWatcher java class.

You can find the source code to on my github account.

Here's the UML schema for the FileSystemWatcher class. (slightly simplified)


$ java -classpath jtail.jar;jopt-simple-4.5.jar --help

$ java -classpath jtail.jar;jopt-simple-4.5.jar -version

$ java -classpath jtail.jar;jopt-simple-4.5.jar ipsum.txt


  • The WatchService, unlike inotify, can only watch directories. Otherwise you receive a java.nio.file.NotDirectoryException.
  • One of the things I like, is the fact that the watchservice, if the filesystem does not support filemonitoring, gracefully falls back to the polling.4
  • I could have used a Path.newByteChannel method of NIO.26, instead of relying on the old RandomAccessFile class. I'll rewrite it someday.
  • I have not done extensive testing, if you find a file and command line that does not work, please do notify me.
  • Where Linux seems to provide filesystem events regularly, Windows seems to be lazy in that regard. I've tried it with a JBoss logfile in Windows, but I only got sporadic events at best. Investigation ongoing.


[1] Wikipedia - inotify
[2] Java tail implementation
[3] Java tail
[4] WatchService (Java Platform SE 7)
[5] GNU core utilities
[6] StackOverflow - Java reading strings from a random access file with buffered input
Manpage : tail
Oracle Tutorial - Reading and Writing Files by Using Channel I/O
Oracle Tutorial - Random access files
Oracle Tutorial - Watching a Directory for Changes
JOpt Simple - a Java command line parsing library

1 comment:

  1. How to Choose a Graphics Tablet that Fits Your Needs

    The first time I tried a Wacom tablet is only comparable with the day I discovered Photoshop, bought my first laptop, tested an Apple computer or I met my girlfriend for the first time - geek joke, honey. But, why are Wacom tablets so special? In this post we'll review the advantages of these tablets.

    Choosing the Correct Drawing Tips

    In my opinion, it must be a Wacom tablet.

    The most relevant technical specification is pressure sensitivity. The more, the better.

    Smaller tablets are better, cheaper, easier to fit into your desktop, and they work just like the bigger ones. I would only choose the big model if you're going to use it for CAD.

    Think about the use you'll give to your tablet. If you want if for painting, coloring and photo manipulation, it will be 100% useful. For drawing, only Cintiq models are really good, and for designing, you need no more than a mouse.

    There are second hand markets or brand new old models. They are really worth checking.