This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
You don't need to invoke a .bat file for this. I see three approaches.
1) Modify your build.xml file so that it removes the target jar file before creating the new one and then using Runtime.exec() (or ProcessBuilder) to execute Ant (without using a .bat file). You can set the working directory using Runtime.exec().
2) Use Runtime.exec() (or ProcessBuilder) to invoke cmd.exe and pipe the .bat script content directly into the 'stdin' of the Process object. For this you don't actually need a .bat file; even though slightly less flexible you can just hard code the script into your Java code.
3) I know there is a library to allow one to invoke Ant directly and modify your Ant script so that it removes the target jar before creating the new one. Google is your friend.
My preference is for 3.
P.S. Your code fragment suggests that you have not read and implemented the recommendations in the 4 sections of http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . You may not think implementing the recommendations necessary and you will get away with it much of the time but at some point the traps will spring out of hiding and bite you.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Prime wrote:Ant is its own library. It's written in Java, so all you need to do is add it to the class path and invoke the main method. Or instantiate the Ant class (no idea which one it is) and call its methods.