Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Ant won't delete a file - any ideas?

I've been experiencing this problem for the last couple of days. Basically, when I run "ant clean" on my project, I get the following error:

file:d:/source/appfuse/build.xml:746: 
  Unable to delete file D:\source\appfuse\dist\appfuse-common.jar

I can delete the "dist" directory in Explorer, and also using "rm -r dist" in Cygwin. Any ideas why Ant is choking on this all of a sudden?

Posted in Java at Dec 26 2002, 05:05:32 PM MST 18 Comments
Comments:

Chances are that your build is using that JAR file in a classpath to <javac> or some other task, and Windows JVM locks JAR files from being deleted.

Posted by Erik Hatcher on December 27, 2002 at 06:02 AM MST #

Hmmm, even if nothing is running eh? They're referenced in my classpath, and I <em>believe</em> I got this code from your sample.

Posted by Matt Raible on December 27, 2002 at 06:18 PM MST #

I've seen this before - NetBeans (and Forte) grab onto jars from time to time and won't let go. I've never had the problem with Eclipse of IDEA.

Posted by Chris Reeves on December 27, 2002 at 06:25 PM MST #

I was able to fix it by removing the following from "ejb.compile.classpath" <code> <pathelement location="${dist.dir}/${webapp.name}-common.jar"/> </code> But since I still need my common classes in my classpath for compiling the "src/ejb" I added it to my "additional.src.dirs". <code> <param name="additional.src.dirs" value="${build.dir}/ejb/gen;src/common"/> </code> Erik - is this what you recommend?

Posted by Matt Raible on December 27, 2002 at 06:26 PM MST #

Yup, that looks good.

Posted by Erik Hatcher on December 29, 2002 at 11:36 AM MST #

Yeah, same thing happened with me. My main Ant build script (within eclipse) generates a jar file that is subsequently used by other build scripts. Those other child build scripts had a reference to the generated jar : <path id="module.class.path"> <pathelement location="${lib.dir}/${generated}.jar"/> </path> Once the child build script is invoked. The ${lib.dir}/${generated}.jar is locked (may be by Ant plugin within eclipse - not sure). So next time when I try to run my main build script it complains : unable to delete ${lib.dir}/${generated}.jar. The problem was solved by keeping two copies of the $(generated}.jar one in ${base.dir} and another in ${lib.dir} and refering ${base.dir}/${generated}.jar in classpath of child build script.

Posted by Yogesh on January 15, 2003 at 09:40 PM MST #

Yeah, same thing happened with me. My main Ant build script (within eclipse) generates a jar file that is subsequently used by other build scripts. Those other child build scripts had a reference to the generated jar
<path id="module.class.path">
<pathelement location="${lib.dir}/${generated}.jar"/>
</path>
Once the child build script is invoked. The ${lib.dir}/${generated}.jar is locked (may be by Ant plugin within eclipse - not sure). So next time when I try to run my main build script it complains : unable to delete ${lib.dir}/${generated}.jar.

The problem was solved by keeping two copies of the $(generated}.jar one in ${base.dir} and another in ${lib.dir} and refering ${base.dir}/${generated}.jar in classpath of child build script.

                

Posted by Yogesh on January 15, 2003 at 09:41 PM MST #

Behold my tale of woe:

I had the same problem just with my own homemade Ant script (not an Eclipse problem or anything like that). I tried everything from file access to voodo incantations and nothing seemed to work.

Then one day as I was just about to give up and explore a new career in vetrenary psycology, it dawned on me... Ant is written in Java! That means it uses classpath. That means if my .jar file is in the classpath than Ant's JVM is probably preloading it, and if its in use, then OF COURSE you can't delete it.

<div style="background:#ffff00">Sure enough, my classpath was ".;c:\jdk;yadda yadda" I removed the ".;" and it works every time!</div>

Well, Vaya Con Dios,
-Kurt

Posted by Kurt on October 26, 2004 at 04:46 PM MDT #

Waowwww ! Thank you Kurt ! I've been having headaches for months with this piece of ####.

Posted by Tom on February 04, 2005 at 04:01 PM MST #

Oh man, what a pain. I was trying to figure out why my clean wasn't deleting a jar file. It turned out that I had "." in my classpath, and the jar file was in my current directory...

Posted by Joel on February 17, 2005 at 06:07 PM MST #

I had a build.xml with <delete>, removing it( and only using owerwrite ) fixed this for me :)

Posted by Logi Helgu on September 06, 2005 at 01:18 PM MDT #

In fact, this is because you have execute some other .bat in the same dos window, some .bat script use the CLASSPATH which contains what you just want to delete in your ant build file. So you just need to open another DOS window to execute your ant task, it should be OK!

Posted by Zhao Ke on November 01, 2006 at 08:44 AM MST #

I had the same problem as Joel (February 17, 2005), namely that I had "." in my system classpath and Ant would not delete "foo.jar" from the current directory. Thanks, Joel and Kurt. But note: if I wanted to load classes from my jar, I would have had to put "./foo.jar" in my classpath, not just "."; is Ant adding *.jar to the classpath? If so, it is certainly doing me no favor. Is this a documented feature of Ant?

Posted by Bob P. on November 25, 2008 at 08:31 PM MST #

I had this problem with NetBeans IDE. In my case , a web project was dependant on a jar file , so the file was placed in $ProjectHome\WEB-INF\lib. However ,when NetBeans starts its work, if the directory contained the jar file , it locks the jar file , so the file can not be deleted. The simple workarround is to delete the file before launching NetBeans IDE. After that the project can be rebuild a couple of times and the jar file is not locked. Hope this will be helpful regards , Vasil.

Posted by Vasko on October 27, 2009 at 12:26 PM MDT #

I had this problem with NetBeans IDE. In my case , a web project was dependant on a jar file , so the file was placed in $ProjectHome\WEB-INF\lib. However ,when NetBeans starts its work, if the directory contained the jar file , it locks the jar file , so the file can not be deleted. The simple workarround is to delete the file before launching NetBeans IDE. After that the project can be rebuild a couple of times and the jar file is not locked. Hope this will be helpful regards , Vasil.

Posted by Vasko on October 27, 2009 at 12:27 PM MDT #

Ant added deleteonexit to the delete task in 1.6.2. Look at this. It fixed my problem.

Posted by Thom on February 02, 2010 at 01:51 PM MST #

Thanks so much! it was definitely that the classpath was referring to the jar file!

Posted by Kenny Cason on March 31, 2011 at 06:18 PM MDT #

deleteonexit worked for me, thanks Thom.

Posted by Frank on March 04, 2013 at 08:50 PM MST #

Post a Comment:
  • HTML Syntax: Allowed