I am facing an issue in the process of porting code from a Linux (Ubuntu LTS 12.4) environment to Windows Server 2008.
I n
In Java, file mappings are garbage collected, and there is no supported way to forcibly destroy a mapping.
From the FileChannel.map() documentation:
The buffer and the mapping that it represents will remain valid until the buffer itself is garbage-collected.
A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping.
In Sun's JDK, you can test that this is indeed the culprit by forcibly destroying the mapping just before doing the file move:
import sun.nio.ch.DirectBuffer;
import sun.misc.Cleaner;
[...]
if (byteBuffer.isDirect()) {
Cleaner cleaner = ((DirectBuffer) byteBuffer).cleaner();
cleaner.clean();
}
// move file
The error message says it all. Windows can't delete or rename an open file like any Unix can, and you're opening workFile
and then immediately trying to rename it. You'll have to close the stream first.