Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Problem in creating a automatic build using java , batch(.bat) file . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in creating a automatic build using java , batch(.bat) file ." Watch "Problem in creating a automatic build using java , batch(.bat) file ." New topic
Author

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

Brajesh Kumar
Greenhorn

Joined: Jan 10, 2011
Posts: 2
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

Joined: Jan 10, 2011
Posts: 2
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

Joined: Sep 07, 2004
Posts: 781

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 Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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: 38001
    
  22
. . . and welcome to JavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in creating a automatic build using java , batch(.bat) file .
 
Similar Threads
Ant version in runme.jar manifest
No Display when running the jar
Are these the correct steps to installing & running Xerces?
JBoss beginners guide
How to set up JSDK2.1 server