views:

3188

answers:

5

Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says,

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

In my case, as part of an upgrade procedure, I need to move (rename) a directory that may contain gigabytes of data (lots of subdirectories and files of varying sizes). The move is always done within the same partition/drive, so there's no real need to physically move all the files on disk.

There shouldn't be any file locks to the contents of the dir to be moved, but still, quite often, renameTo() fails to do its job and returns false. (I'm just guessing but perhaps some file locks expire somewhat arbitrarily on Windows.)

Currently I have a fallback method that uses copying & deleting, but this sucks because it may take a lot of time, depeding on the size of the folder. I'm also considering simply documenting the fact that the user can move the folder manually to avoid waiting for hours, potentially. But the Right Way would obviously be something automatic and quick.

So my question is, do you know an alternative, reliable approach to do a quick move/rename with Java on Windows, either with plain JDK or some external library. Or if you know an easy way to detect and release any file locks for a given folder and all of its contents (possibly thousands of individual files), that would be fine too.


Edit: In this particular case, it seems we got away using just renameTo() by taking a few more things into account; see this answer.

A: 

You may try robocopy. This is not exactly "renaming", but it's very reliable.

Robocopy is designed for reliable mirroring of directories or directory trees. It has features to ensure all NTFS attributes and properties are copied, and includes additional restart code for network connections subject to disruption.

Anton Gogolev
Thanks. But since robocopy is not a Java library, it probably wouldn't be very easy to (bundle it and) use it from my Java code...
Jonik
A: 

To move/rename a file you can use this function:

BOOL WINAPI MoveFile(
  __in  LPCTSTR lpExistingFileName,
  __in  LPCTSTR lpNewFileName
);

It is defined in kernel32.dll.

Blindy
I feel going through the trouble of wrapping this in JNI is greater than the effort required to wrap up robocopy in a Process decorator.
Kevin Montrose
Just offering alternatives!
Blindy
yep, this is the price you pay for abstraction - and when it leaks, it leaks good =D
Chii
Thanks, I might consider this if it doesn't get too complicated. I haven't ever used JNI, and couldn't find good examples of calling a Windows kernel function on SO, so I posted this question: http://stackoverflow.com/questions/1000723/what-is-the-easiest-way-to-call-a-windows-kernel-function-from-java
Jonik
You could try a generic JNI wrapper like http://johannburkard.de/software/nativecall/ as this is a pretty simple function call.
Peter Smith
A: 

I know it sucks, but an alternative is to create a bat script which outputs something simple like "SUCCESS" or "ERROR", invoke it, wait for it to be executed and then check its results.

Runtime.getRuntime().exec("cmd /c start test.bat");

This thread may be interesting. Check also the Process class on how to read the console output of a different process.

Ravi Wallau
+1  A: 

For what it's worth, some further notions:

  1. On Windows, renameTo() seems to fail if the target directory exists, even if it's empty. This surprised me, as I had tried on Linux, where renameTo() succeeded if target existed, as long as it was empty.

    (Obviously I shouldn't have assumed this kind of thing works the same across platforms; this is exactly what the Javadoc warns about.)

  2. If you suspect there may be some lingering file locks, waiting a little before the move/rename might help. (In one point in our installer/upgrader we added a "sleep" action and an indeterminate progress bar for some 10 seconds, because there might be a service hanging on to some files). Perhaps even do a simple retry mechanism that tries renameTo(), and then waits for a period (which maybe increases gradually), until the operation succeeds or some timeout is reached.

In my case, most problems seem to have been solved by taking both of the above into account, so we won't need to do a native kernel call, or some such thing, after all.

Jonik
I'm accepting my own answer for now, as it describes what helped in our case. Still, if someone comes up with a great answer to the more general issue with renameTo(), feel free to post and I'll be happy to reconsider the accepted answer.
Jonik
+2  A: 

The original post requested "an alternative, reliable approach to do a quick move/rename with Java on Windows, either with plain JDK or some external library."

Another option not mentioned yet here is v1.3.2 or later of the apache.commons.io library, which includes FileUtils.moveFile().

It throws an IOException instead of returning boolean false upon error.

See also big lep's response in this other thread.

MikeC
Also, it looks like JDK 1.7 will include better filesystem I/O support. Check out java.nio.file.Path.moveTo():http://java.sun.com/javase/7/docs/api/java/nio/file/Path.html
MikeC