1

I want to rename folders using Java 7. I have tried using:

srcDir.renameTo(desDir);

But from what I can understand (and personal experience) .renameTo only works for renaming files. I have also tired using:

FileUtils.moveDirectory(srcDir, desDir);

But I get this error:

Exception in thread "main" java.io.IOException: Unable to delete file: C:\webapps\37\WEB-INF\lib\stax-api-1.0-2.jar
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2279)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1535)
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2270)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1535)
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2270)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1535)
at org.apache.commons.io.FileUtils.moveDirectory(FileUtils.java:2756)
at batDel.batDelJavaC.main(batDelJavaC.java:52)

Note: I am running on a Tomcat 8 server, but have stopped it before running my project.

now_world
  • 940
  • 7
  • 21
  • 56
  • 2
    do you have permissions to delete it? and does the directory exist? – Ankur Shanbhag Jan 08 '15 at 19:58
  • Yes the directory does exist. Yes I have the permissions to delete. – now_world Jan 08 '15 at 20:02
  • @Smac89 I am actually trying to delete the "C:\webapps\37" folder. I don't know why I am getting an "Unable to delete file:" error. – now_world Jan 08 '15 at 20:03
  • are you trying to delete a jar that is loaded on a running Tomcat ? – elsadek Jan 08 '15 at 20:04
  • Most likely you cant delete the jar file and thats why you cant delete the folder. Try to delete jar file manually first and try then. – VGe0rge Jan 08 '15 at 20:05
  • I have stopped the Tomcat server from running, and there are hundreds of .jar files. So if I delete one, it'd complain about another. – now_world Jan 08 '15 at 20:09
  • 1
    Even if you stopped tomcat you have to be sure that all the files are closed, otherwise we will never be able to rename/delete the folder – VGe0rge Jan 08 '15 at 20:18
  • @VGe0rge I added the answer code for the question here [link](http://stackoverflow.com/questions/1390592/java-check-if-file-is-already-open) And it's saying that the file is locked. Now what? – now_world Jan 08 '15 at 20:26
  • 1
    Check the post of @Mark Meuer. You have to find out which proccess keeps the file locked and kill it. – VGe0rge Jan 08 '15 at 20:32

1 Answers1

2

It is very likely that the .jar file in question is actually opened by some process like the application server. Until it closes the file, you won't be able to move or delete it.

On Windows (which it looks like you're using) you can use a utility like Process Explorer from SysInternals to see if some process has the file open.

Mark Meuer
  • 7,200
  • 6
  • 43
  • 64
  • To whoever down-voted my answer: please comment to explain your down-vote so that I may improve or remove the answer. – Mark Meuer Jan 08 '15 at 20:04
  • I have stopped the server before running this project. – now_world Jan 08 '15 at 20:05
  • I didn't downvote, but I imagine it's because this answer should be a comment on the op's question, not an answer in and of itself. Please read [answer] to familiarise yourself with these things. – Software Engineer Jan 08 '15 at 20:06
  • 1
    @user3539592 To check that the file is not locked by another process, try to delete it from the shell. – elsadek Jan 08 '15 at 20:11
  • @elsadek Just tried. Worked perfectly, but only if I ran as admin. That's probably my problem. – now_world Jan 08 '15 at 20:14
  • Yeah your problem probably was that a user proccess was keeping the file locked. – VGe0rge Jan 08 '15 at 21:06