2014-03-26 103 views
0

我想從Java調用build.xml。要做到這一點,我在Java主方法中提到下面的代碼:無法從java main方法調用build.xml

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 

請幫助。

回答

1

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

或者......它沒有找到這個文件:

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

我不太清楚。檢查兩個,看看是否有幫助。

0

Ant在Windows上的入口點是一個批處理腳本,ant.bat。批處理腳本必須在命令解釋器中運行,例如cmd.exe

試試這個:

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