无法从java main方法调用build.xml

时间:2014-03-26 11:30:11

标签: java xml ant

我想从Java调用build.xml。为此,我在Java main方法中提到了下面的代码:

Process process;
        try {
                process = new ProcessBuilder("ant","-f" ,"D:/Selenium/Workspace/test_project/build.xml").start();
                process.waitFor();
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                while ((line = br.readLine()) != null) 
                {
                    System.out.println(line);
                }

                } catch (Exception e) 
                {
                    e.printStackTrace();
            }

但是我得到了错误:

java.io.IOException: Cannot run program "ant": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at com.gui.test.TestClass_Base.main(TestClass_Base.java:155)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 2 more

请帮忙。

2 个答案:

答案 0 :(得分:1)

似乎ant不在你的道路上。所以它找不到它。

或者......它找不到这个文件:

D:/Selenium/Workspace/test_project/build.xml

我不太确定。检查两者,看看是否有帮助。

答案 1 :(得分:0)

Windows上Ant的入口点是批处理脚本ant.bat。批处理脚本必须在命令解释器中运行,例如cmd.exe

试试这个:

new ProcessBuilder("cmd.exe","/c","ant","-f","D:/Selenium/Workspace/test_project/build.xml")