• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in creating a automatic build using java , batch(.bat) file .

 
Brajesh Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am tried creating an automatic build process using a java scheduler which triggers the automatic build process at regular interval.

The java code--->
timer.schedule(new TimerTask() {
public void run() {

try {File dir = new File("D:\\");
Runtime.getRuntime().exec("D:\\build.bat", null, dir);

} catch (Exception e) {
e.printStackTrace();
}}}, delay, 60000);


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.




 
Brajesh Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Addingthe build.xml for reference--->

<?xml version="1.0"?>
<project name="ApplicationContextExample" basedir="." default="jar">

<property name="src" value="src"/>
<property name="output" value="bin"/>

<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="java"/>
</javac>
</target>

<target name="jar" depends="compile">
<jar destfile="jar\ApplicationContextExample.jar">
<fileset dir="${output}"/>
<fileset file="${src}/applicationContextExample.xml" />
</jar>
</target>

<target name="clean">
<delete dir="${output}"/>
</target>

<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>

<target name="run">
<java classname="ApplicationContextExample"
classpath="jar\ApplicationContextExample.jar"
classpathref="java"
fork="true" />
</target>

<path id="java">
<fileset file="jar\spring.jar" />
<fileset file="jar\commons-logging-1.1.1.jar" />
<fileset dir="${src}" />
</path>
</project>
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic