I'm investigating the best (cross-platform) way to lock some files that are used by a Java application.
The worst-case scenario is that the program quits unexpectedly (user forces termination, or unclean exit). In this case, what is the best way to ensure file locks are released, so they don't affect any subsequent access by a program?
I've looked at using FileLock, but an unclean termination could leave the file in a locked unusable state.
Is there a way to resolve this issue with code? either catching such problems to first 'clean-up', or on a subsequent run of the program?
I don't think FileLock has the problem you think it has. According to the documentation it uses the OS file lock functionality which I think means most OSs would clean up any locks held by the process when it quits (though you may want to do some tests to verify this).
Otherwise you need a way to determine whether the lock is stale or not.
A common method for handling this with things like shell scripts is to put the process' ID (PID) in the lock file. This way other apps which see the lock file can verify whether the lock is still valid by checking for that process running. If the process isn't running the lock can be cleaned up.
Though I don't know of a way to do this through Java...