简体   繁体   English

如何使用Runtime.getRuntime()。exec()执行ant并关闭命令提示符

[英]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.

相关问题 使用Runtime.getRuntime()。exec无法打开命令提示符 - Command prompt doesn't open with Runtime.getRuntime().exec Java/Kotlin 使用可见的命令提示符运行 Runtime.getRuntime().exec() - Java/Kotlin run Runtime.getRuntime().exec() with visible command prompt 使用Runtime.getRuntime()。exec()在运行时在Java中执行sql语句 - execute sql statements in java at runtime using Runtime.getRuntime().exec() 为什么 Runtime.getRuntime().exec(startupOracle); 没有完全执行命令 - why Runtime.getRuntime().exec(startupOracle); does not fully execute command 无法通过java Runtime.getRuntime()。exec()执行CURL命令 - Unable to execute CURL command through java Runtime.getRuntime().exec() 如何通过 Runtime.getRuntime().exec() 在命令行应用程序中执行命令 - How to execute commands in a command-line application through Runtime.getRuntime().exec() 使用Runtime.getRuntime()。exec(String [])在Java中运行Windows命令 - Running Windows Command in Java Using Runtime.getRuntime().exec(String[]) 使用Runtime.getRuntime()。exec()执行Java文件 - Execute java file with Runtime.getRuntime().exec() 在命令中使用带有双 qoute 的 java Runtime.getRuntime().exec() 进行 Rsync - Rsync using java Runtime.getRuntime().exec() with double qoute in command 使用“ Runtime.getRuntime()。exec()”执行命令行 - Executing command line using “Runtime.getRuntime().exec()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM