[英]How to execute ant and close the command prompt using Runtime.getRuntime().exec()
I want to execute an ant file from java. 我想从Java执行ant文件。 So i decided to use
Runtime.getRuntime().exec()
to achieve this. 所以我决定使用
Runtime.getRuntime().exec()
来实现这一点。 My java file will looks something like below, 我的Java文件如下所示:
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. 我的目标是运行带有少量参数的E:/ ant_demo路径中可用的ant文件。 Once after completing the ant file, the remaining code should be executed.
完成ant文件后,应执行其余代码。
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. 当我运行此代码时,将为ant打开一个单独的命令提示符窗口,并且在ant文件完成之前,其余代码也将并行执行。 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. 进行此更改之后,即使在完成ant之后,也不会执行其余代码,并且用于ant的命令提示符将保持打开状态。 When i close the command prompt used for ant manually, then the remaining codes are getting executed.
当我手动关闭用于ant的命令提示符时,其余代码将被执行。
How to make the command prompt used by ant to close automatically? 如何使ant使用的命令提示符自动关闭? or how to change my code to run the ant file and execute the remaining code once after the ant is completed?
还是如何更改我的代码以运行ant文件并在ant完成后执行一次剩余的代码?
I tried to achieve this in many ways, but still facing this problem. 我试图以多种方式实现这一目标,但仍然面临着这个问题。
You can run the Ant script by invoking the normal Ant executable (a ProcessBuilder
can do). 您可以通过调用普通的Ant可执行文件(
ProcessBuilder
可以执行)来运行Ant脚本。 The ANT_HOME
environment variable usually points to the Ant installation so you can construct the path to the executable from it: ANT_HOME
环境变量通常指向Ant安装,因此您可以从中构建可执行文件的路径:
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
. 请注意,在调用
ProcessBuilder#start()
之后,将检索输入流以读取Ant命令的输出并将其打印到System.out
。 See Java Process with Input/Output Stream for more info. 有关更多信息,请参见带有输入/输出流的Java进程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.