I am trying to invoke a batch file "build.bat" using java code.The code invoke the build file at regularly aand it is on a infinite loop.
The build.bat file contains---->
del D:\Brajesh\ApplicationContextExample\jar\ApplicationContextExample.jar
cd D:\Brajesh\ApplicationContextExample
ant -buildfile build.xml
Now the problem is that the build file is creating a jar file which is not created till the java code execution is stopped.
I tried no of ways like -->
:loop
if not exist D:\Brajesh\ApplicationContextExample\jar\ApplicationContextExample.jar goto loop in batch file AND
do{ }while(!file1.exists()); in java code BUT nothing is working.
Please help with any suggestion solution.
Thanks in advance.
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.
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.
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.
:-)) I knew it was easy but ...
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
4
posted
0
. . . and welcome to JavaRanch
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Problem in creating a automatic build using java , batch(.bat) file .