• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic