1

I want to execute an ant file from java. So i decided to use Runtime.getRuntime().exec() to achieve this. My java file will looks something like below,

Process p = Runtime.getRuntime().exec("cmd /c start ant mytarget -Darg1="+arg1+" -Darg2="+arg2+" ", null, new File("E:/ant_demo"));

System.out.println("Ant file executed");
...
...
..
System.out.println("Completed");

My goal is to run the ant file available in the path E:/ant_demo with few arguments. Once after completing the ant file, the remaining code should be executed.

When i run this code, a separate command prompt window is opened for ant and the remaining code is also getting executed in parallel before the ant file is completed. In order to make the code to wait until the ant is completed, i changed my code as below,

    Process p = Runtime.getRuntime().exec("cmd /c start /wait ant mytarget -Darg1="+arg1+" -Darg2="+arg2+" ", null, new File("E:/ant_demo"));
    p.waitFor();

    System.out.println("Ant file executed");
    ...
    ...
    ..
    System.out.println("Completed");

After this change, even after the ant is completed the remaining code is not getting executed and the command prompt used for ant is stayed open. When i close the command prompt used for ant manually, then the remaining codes are getting executed.

How to make the command prompt used by ant to close automatically? or how to change my code to run the ant file and execute the remaining code once after the ant is completed?

I tried to achieve this in many ways, but still facing this problem.

M A
  • 71,713
  • 13
  • 134
  • 174
Jugi
  • 1,244
  • 3
  • 23
  • 51
  • Do you have to invoke a `cmd` window? Can't you execute the script without `cmd /c ...`? – M A Apr 27 '15 at 15:06
  • Hi manouti, i removed the start option, but still the remaining codes are not getting executed. – Jugi Apr 27 '15 at 15:07
  • @Manouti, it is not specific to run using cmd window. is there any other way to run ant file using java other than cmd winodw ? – Jugi Apr 27 '15 at 15:08

1 Answers1

1

You can run the Ant script by invoking the normal Ant executable (a ProcessBuilder can do). The ANT_HOME environment variable usually points to the Ant installation so you can construct the path to the executable from it:

String antHome = System.getenv().get("ANT_HOME");
String antExecutable = antHome + File.separator + "bin" + File.separator + "ant.bat";

List<String> command = new ArrayList<String>();
command.add(antExecutable);
command.add("mytarget");
command.add("-Darg1="+arg1);
command.add("-Darg2="+arg2);
command.add("-propertyfile");
command.add("myproperty.properties");

ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(new File("E:/ant_demo")); // set working directory
Process process = processBuilder.start(); // run process

// get an input stream connected to the normal output of the process
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while (( line = reader.readLine ()) != null) {
    System.out.println(line);
}
System.out.println("Ant file executed");
...
...
System.out.println("Completed");

Notice that after ProcessBuilder#start() is called, an input stream is retrieved to read the output of the Ant command and print it to System.out. See Java Process with Input/Output Stream for more info.

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174
  • how to pass the propertyfile name along with target name. Using command prompt i will do somethink like `ant -propertyfile myproperty.properties targetName`. The above code is not loading this property. How to achieve this ? – Jugi Apr 27 '15 at 15:45
  • @Jugi Just update the `command` list by adding these options. – M A Apr 27 '15 at 15:48
  • i have added `command.add(antExecutable);` `command.add("-propertyfile");` `command.add("myproperty.properties");` `command.add("targetName");`. But it is not working properly. – Jugi Apr 27 '15 at 15:49
  • @Jugi Could you update with what you tried and the error / unexpected behavior you're getting? – M A Apr 27 '15 at 15:53
  • 1
    my bad i passed the wrong arguments. This is workin perfect as i wanted. Thanks a lot :) – Jugi Apr 27 '15 at 16:01