It’s possible that the reason that renaming failed is that the file is still open. Even if you are closing the file, it could be held open because of (for example):
- A file handle is inherited by a subprocess of your process
- An anti-virus program is scanning the file for viruses, and so has it open
- An indexer (such as Google Desktop or the Windows indexing service) has the file open
To help find out what is keeping the file open, use tools such as FileMon and Handle.
Update: A tool such as Unlocker may not help, if the file is only held open for a very short time (as would be the case for an anti-virus scan). However, if javaw.exe is shown as having the file open, that’s your problem right there.