permaculture playing cards*
The moose likes Ant, Maven and Other Build Tools and the fly likes unable to delete or rename old file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "unable to delete or rename old file" Watch "unable to delete or rename old file" New topic
Author

unable to delete or rename old file

Sven Thor
Ranch Hand

Joined: Jan 31, 2004
Posts: 34
When I try to either delete or update an existing .jar file my ant build fails with the message "Unable to delete file C:\ejb_titan\titan.jar" and "Unable to rename old file(C:\ejb_titan\titan.jar)".

I am using Windows XP and the same build file works fine on my laptop which also uses Windows XP. So I guess there is a problem with the setup of the machine I am using, but I am not able to figure it out.

BTW, I have seen the problem reported previously on this forum and also on a couple of other forums on the net but without any solution.

Any help will be much appreciated,

br
Sven

Here are the jar and clean targets.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Windows is very picky about file sharing. If it thinks something else has the file open (it's not always very good about keeping track), it will disallow the operation.

Do you have an IDE that could have the file open? A server (like Tomcat)?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sven Thor
Ranch Hand

Joined: Jan 31, 2004
Posts: 34
No, this happens also when no IDE or Tomcat is running. So there must be some other process using the file. How can I find out which ? I need to know which process to shut down in the Windows Task Manager (ctrl+alt+del).

BTW, I noticed that I can't remove the Read Only attribute from ANY folder on my system. Is it correct that WinXP just ignores this attribute ? And is there a way to remove it, I've tried right-clicking the file and accessing the File properties dialog.

Sven
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Bear Bibeault is correct, I think. Windose makes a lot of decisions for you, even it is not always correct.

I was struggling with similar problems these two days. When ant cannot do it, then try use your hand to delete it, if you cannot, it is clear it is "Windose".

How to fight with your "Windose"? Try the followings in order:
1) Close all possible apps use that file/dir.
2) Close cmd or cygwin windows
3) Close Windows Explorer, then reopen.
4) If still not work, shut-down your PC, count 1 to 10, then start again. It worked

Haha
[ June 21, 2005: Message edited by: Roseanne Zhang ]
Sven Thor
Ranch Hand

Joined: Jan 31, 2004
Posts: 34
I am able to delete the file manually, so the problem must in some way be related to Ant.

Having to delete the file manually every time you deploy becomes extremely tedious after a while.

Sven
Joe Chitrady
Greenhorn

Joined: Jul 02, 2005
Posts: 1
I too was struggling for a long time and finally this is the solution for me which I think is applicable to some of us.
This is not related to windows have a lock on the file as the file can be renamed, deleted manually. I believe it is a bug in Ant itself.

See example below as illustration. If in your target tag your have a depends attribute to a target that refer to the jar file you want to update then you will have this issue. The solution is to remove it from the depends attribute, this would mean that you have to execute all the dependent targets manually first and then the target to build that jar file.

For example:

<target name="buildMyJar" depends="compile">
^^^^^^^^^^^^^^^^^^

<zip destfile="myJar.jar" basedir="${build}"
includes="com/xxx/order/**" update="true"/>
</target>

<target name""compile">
<javac srcdir="${src}" debug="on" destdir="${build}" includes="**"
classpath="${INSTALL_PATH}\lib\myJar.jar;"/>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</target>


To fix it then change the buildMyJar target to:

<target name="buildMyJar"> <<< remove the depend attribute
<zip destfile="myJar.jar" basedir="${build}"
includes="com/xxx/order/**" update="true"/>
</target>
And execute compile before buildMyJar separately. Till Ant is fixed, I think this is the best workaround.


Hope this helps.
Good luck.

[ July 02, 2005: Message edited by: Joe Chitrady ]
[ July 02, 2005: Message edited by: Joe Chitrady ]
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Your first suspect for what has the file locked should be your Antivirus package.

It has happened to me .......

Guy
Jerome Garcia
Greenhorn

Joined: Jan 07, 2009
Posts: 1
Guy Allard wrote:Your first suspect for what has the file locked should be your Antivirus package.

It has happened to me .......

Guy


YES! It was indeed the Antivirus package that was causing the file conflict for me as well. Disabling the realtime antivirus scanning is what completely solved the problem for me!

Amazing.

Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

Jerome Garcia wrote:
Guy Allard wrote:Your first suspect for what has the file locked should be your Antivirus package.

It has happened to me .......

Guy


YES! It was indeed the Antivirus package that was causing the file conflict for me as well. Disabling the realtime antivirus scanning is what completely solved the problem for me!

Amazing.



also i'll recommend to check wid file permissions before deleting them...


Dhruva
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Well, that was educational!

Incidentally, Windows has a very pessimistic file locking mechanism, and unlike Unix, it locks on the file's name, not on the directory node. So I think you'll find you can't rename a locked file.

In fact, that's one of the reasons why Windows is so obsessive about reboots related to software installation, Less constricted OS's would typically rename or delete a "dll" that needed replacing, copy in the new DLL, then set up for actual deletion when the old one was unlocked (generally at reboot).. Since the lock is on the name and not the data, Windows doesn't have that luxury. Instead the new DLL is queued up and installed during the reboot process before the locks are established.


Customer surveys are for companies who didn't pay proper attention to begin with.
Otto Kar
Greenhorn

Joined: Mar 22, 2011
Posts: 1
Thanks Tim,

your post saved my day. I could solve the problem.


What I learnt:

If you use any classpath in any target of your build on your Windows(!) machine,
you should never try to delete any of the jars that are used in these classpaths
at any time later during this Ant build because all the jars remain locked.

If you think your problem is another one, double-check this scenario by starting your build
with option -version and scan the output for "class" or the name of your jar file.
You might be surprised.

Good luck!
php gen
Greenhorn

Joined: Jan 02, 2012
Posts: 1
Hii,I was having these same issues when i got this same error till i found this tool

http://longpathtool.com
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

"php gen", please see your private messages regarding an important administrative matter


JBoss In Action
ayman ronaldi
Greenhorn

Joined: Jan 04, 2012
Posts: 1
thanks phpgen

your tool helped on my situation

i was trying first rebooting but with no luck

once again thanks
Gerald Lampi
Greenhorn

Joined: Sep 11, 2009
Posts: 2
I had the same problem (for years). We have a large ant build script and various files would be locked. Mostly caused by eclipse and WindowBuilder Pro.
I finally fixed it programmatically by calling freeware LockHunter.exe version 2.0.2 command line from a .bat file BEFORE any other ant targets. Here are all of the details:

Old ant target:

Modified ant target:

New ant targets:

Why the if/else? In our shop, we do a nightly build that does not have access to LockHunter, so if the file signJarsViaBatFile.txt is present on the machine that ant is running on, then LockHunter will run. This allows developers to allow ant to run differently on their desktop. Oh, our build signs over 125 jars, and we also have a .bat file to sign them from command line using java jarsigner, not ant signjar (this was a huge speed increase in jar signing; a build that took 6 minutes now runs in 1.9 minutes).
To use if/else, I think you also need this in the ant script:


Here is the C:\Junk\UnlockAll.bat file:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to delete or rename old file