23

I have an ant build that makes directories, calls javac and all the regular stuff. The issue I am having is that when I try to do a clean (delete all the stuff that was generated) the delete task reports that is was unable to delete some files. When I try to delete them manually it works just fine. The files are apparently not open by any other process but ant still does not manage to delete them. What can I do?

Honza
  • 4,349
  • 2
  • 24
  • 40

15 Answers15

15

I encountered this problem once. It was because the file i tried to delete was a part of a classpath for another task.

Shimi Bandiel
  • 5,773
  • 3
  • 40
  • 49
  • 2
    @StefanSprenger you have to execute your tasks in the right order – CMS Aug 19 '14 at 10:01
  • 2
    It is not always the reason for this problem. Sometimes you just have to restart your system and everything works fine. In this case, probably the reason is windows falsely thinks a process is using those files. – Arashsoft Dec 17 '15 at 18:41
  • @ShimiBandiel, thanks, seems logical. I tried to move the file manually, and `Windows 10` told me that it is being used by Eclipse. I close Eclipse, move the file, and resume my ant build. What annoys me is that i need that file on the classpath... – jumping_monkey Jun 16 '20 at 09:27
9

It depends ...

  • The Ant process doesn't have enough permissions to delete the files (typically because they were created by a different user, perhaps a system user). Try running your Ant script as an administrative user, using Run As.
  • Windows is really bad at cleaning up file locks when processes die or are killed; consequently, Windows thinks the file is locked by a process that died (or was killed). There's nothing you can do in this situation other than reboot.
  • Get better tools to inspect your system state. I recommend downloading the SysInternals tools and using them instead of the default Windows equivalents.
Craig Trader
  • 15,507
  • 6
  • 37
  • 55
  • I agree with point 3. sysinternals handle.exe is invaluable as a command line tool to analyse this sort of problem. – Richard A Oct 24 '08 at 01:30
  • Okay, so sysinternals shows me that javaw.exe has a handle on a file or directory, now what? – Snekse Mar 11 '11 at 15:25
5

Using Ant Retry task has helped me. I've just wrapped it around the Delete Task.

Artem Nakonechny
  • 121
  • 1
  • 3
  • 4
3

I faced the same problem.
I didn't have any classpath set to or antivirus running on my machine.
However, the ANT version I was using was 32 bit and the JDK I installed was 64 bit.
I installed a 32 bit JDK and the issue was resolved.

Vinila
  • 31
  • 1
3

You don't say if your build is run as the currently logged on user. If not, the fact that explorer.exe or other process has the directory shown can cause it to be locked as well. But deleting it in that same explorer.exe process would succeed. Try Unlocker from http://ccollomb.free.fr/unlocker/ to see what processes have the files/directories locked.

BQ.
  • 9,393
  • 3
  • 25
  • 35
3

Is there something from the Ant process that is holding the files (or directory) open? This would cause the situation where you could delete them after running ant, but not during.

John Meagher
  • 22,808
  • 14
  • 54
  • 57
2

Ant versions before 1.8.0 have a bug which leads to random errors during delete operation. Try using Ant 1.8.0 or newer.

You can see the bug details here https://issues.apache.org/bugzilla/show_bug.cgi?id=45960

Sergey
  • 3,253
  • 2
  • 33
  • 55
1

In my case my ant clean was failing from Eclipse, unable to remove build files. I see this from time to time. Usually succeeds on a repeat attempt. This time no. Tried running ant clean from command line, failed Unable to delete"unable to delete". It must have been Eclipse holding on to the problem file, when I exited Eclipse, cmd line was able to delete OK.

MikeRoger
  • 766
  • 1
  • 19
  • 25
0

I've been having this problem a lot lately and it's random. One time it works, the next time it doesn't work. I'm using NetBeans (in case that matters) and I've added a lot of extra tasks to build.xml. I was having this problem in the -post-jar task. It would happen when I call unjar on the file, then delete. I suspect that NB is trying to scan the jar and this causes the lock on it.

What worked for me is to immediately rename the jar at the start of -post-jar and add a .tmp extension to it. Then I call unjar on the temp file. When I'm done I rename back to the desired jar name.

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
0

I too had the same problem and was tried of manually deleting the build directories. Finally I solved it by renaming the .jar artifact of my project to a different name from project name itself. For ex: my project was portal and my ant built script use to generate portal.jar, where eclipse ant was not able to delete this portal.jar. When i changed my build.xml to generate my .jar as portalnew.jar, eclipse was able to delete this portalnew.jar next time. Hope this helps.

Sudheer Palyam
  • 2,499
  • 2
  • 23
  • 28
0

You need to delete it manually in Windows. It worked for me. (Usually the files to be deleted are older versions of jar.. For example: if there exists httpcore.4.2.5.ja5r and httpcore.4.3.jar, it will try to delete 4.2.5.jar)

maQbex
  • 3
  • 3
0

i faced this issue as the file the ant was trying to delete was being used by some other service/process. I stopped the service, and then the ant build script did run through.

priya
  • 69
  • 2
0

In my case, I stopped running Java process from Task Manager and re-run the Ant build file. The file was able to delete and build was successful.

ShivaKumar
  • 31
  • 5
0

I am seeing problems like this way too often since I switched to Microsoft Windows 10. Renaming the file immediately before removing it solved it for me:

<rename src="file.name" dest="file.name.old"/>
<delete file="file.name.old" />
fabien-michel
  • 1,873
  • 19
  • 38
momo
  • 5
  • 2
0

For me, I am using mac so I tried sudo before ant cmd, sudo ant clean all and it did work perfectly fine.

As i've read javac will not have access to delete JAR files so you can either sudo it or find alternative.

Milan Patel
  • 104
  • 8