tags:

views:

1624

answers:

5

I have a file I need to rename to that of an existing file. This is a copy, modify, replace original operation on an existing JAR file. I've got the first two steps done, I just need help with the replace original bit. What's the best way to rename the new version of the JAR to that of the old. The old JAR doesn't need preserving and I don't want to have a copy of the new with its initial name sticking around.

I have commons lang and io already, so if there's a method I've missed, that would be great.

+1  A: 

Is there a problem with deleting the old one and renaming the new one?

Michael Myers
+1  A: 

You're going to need to create two java.io.File objects: one for the new file, one for the old file.

Lets call these oldFile and newFile.

oldFile.delete()
newFile.renameTo(oldFile);

Edit: mmyers beat me to it.

R. Bemrose
renameTo is platform dependant, and may not work in every case. You should always check if it succeeded after doing it.
James Van Huis
+1  A: 

Java.io.File.renameTo(java.io.File)

You might need to call File.delete() first on the original file first - some systems won't rename a file onto an existing file.

Alnitak
Just checked on Windows XP and this won't work without deleting the original.
Outlaw Programmer
did it throw an exception or just return false?
Alnitak
+2  A: 

This should get you reasonably close:

public boolean replaceOldJar(String originalJarPath, java.io.File newJar) {
    java.io.File originalJar = new java.io.File(originalJarPath);
    if (!originalJar.isFile()) {
     return false;
    }
    boolean deleteOldJarSucceeded = originalJar.delete();
    if (!deleteOldJarSucceeded) {
     return false;
    }
    newJar.renameTo(originalJar);
    return originalJar.exists();
}
James Van Huis
+1  A: 

I'm not completely sure if you are asking simply how to rename the file in the filesystem or if you also want to reload this new version of your jar?

The rename part sounds easy... just use File.renameTo... unfortunately, there are many platform specific problems related to doing this. Some platforms will not allow you to overwrite an existing file, others will not allow you to rename a file so it changes location onto another partition. If you want to make the process completely safe, you need to do the process yourself by removing the old file first and then renaming the new (or copying if a partition change is possible). This is naturally prone to problems if your application/machine crashes while doing this, since it is no longer an atomic operation. You will thus need to add a check to your applications startup process that looks for rename operations that were in the middle when the crash occured. If you are just updating a single file in this way, it should be pretty easy.

However, if you actually want to reload the jar, there are a few more issues to thing about, but you would need to give a bit more detailed view of the situation to get proper advice on how to do it.

kasperjj