ant error Unable to rename old file to temporary file
Asked Answered
M

5

7

I'm using ant 1.8.0 and java 1.6.0.17 and I'm running into a strange problem.

In my build.xml, I have a simple task that compiles the code

<javac destdir="${dir.build.classes}" debug="on">
    <classpath refid="classpath"/>
    <src path="${dir.src.java}"/>
</javac>

In the "classpath" is a jar, call it library.jar

In a later task, I need to add a few classes to library.jar, which I do like this

<jar destfile="library.jar" update="true" duplicate="fail">
    <fileset dir="${dir.build.classes}">
        <include name="some/class/files"/>
    </fileset>
</jar>

This will fail with the error Unable to rename old file (library.jar) to temporary file

I stuck in a call to handle.exe before and after the javac call, and I can confirm that the java process running ant grabs a file handle to library.jar during the javac call, and it doesn't give it up. This causes my later attempt to update the jar to fail.

Why would ant keep a handle to the jar in a classpath open even after the javac task is complete?

Milky answered 20/2, 2010 at 0:8 Comment(0)
M
5

So I found the answer, after some experimentation. By adding fork="true" to my javac task, the file handle is closed at the end of the task. This allows my jar modification to succeed later in the build.

It's unfortunate though, because I have to remember to add this to every upstream javac task.

Milky answered 23/2, 2010 at 15:17 Comment(0)
I
3

This is a windows locking issue. Any process/thread reading the file will prevent it from being renamed, which is what the zip task is doing, when updating an existing jar file.

I'm guessing that the file handle is being kept open because your using a classpath reference. Maybe the file handles might be closed if you were to explicitly set the javac task's classpath?

Intermix answered 20/2, 2010 at 2:39 Comment(1)
Can you explain what you mean by "explicitly set the javac task's classpath" ? It's a rather large set of jars that is used in several other places, so hard-coding them isn't really an option, and I'm not sure why it would make a difference.Milky
M
1

There is a bug filed for the exact same issue here.

They are saying that this is fixed in Ant version 1.8

Monarchist answered 6/11, 2010 at 11:39 Comment(0)
C
1

I tried latest version of ANT(1.10.7) and its works for me.

This issue has been solved in 1.9.7

WHATSNEW in ant

FileUtils.java

Changchangaris answered 30/3, 2020 at 8:30 Comment(0)
A
-1

It seems related to classpath configuration and The first operation on the jar file keeps it opened. I've resolved this issue by removing "." from my classpath env variable.

Adventurism answered 13/8, 2010 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.