2009-03-05 53 views
96

在我的Java應用程序,我想運行調用「scons -Q implicit-deps-changed build\file_load_type export\file_load_type如何從我的Java應用程序運行批處理文件?

看來,我甚至不能讓我的批處理文件來執行批處理文件。我沒有想法。

這是我在Java中:

Runtime. 
    getRuntime(). 
    exec("build.bat", null, new File(".")); 

以前,我有,我想運行一個Python Sconscript文件,但因爲沒有工作,我決定我會通過批處理文件調用腳本但該方法迄今尚未成功。

回答

155

批處理文件不是可執行文件。他們需要一個應用程序來運行它們(即cmd)。

在UNIX上,腳本文件在文件開始處有shebang(#!)以指定執行它的程序。在Windows中雙擊是由Windows資源管理器執行的。 CreateProcess對此不知情。

Runtime. 
    getRuntime(). 
    exec("cmd /c start \"\" build.bat"); 

注意:使用start \"\"命令,一個單獨的命令窗口將被與一個空白標題和從該批處理文件的任何輸出將顯示有打開。它也應該只用'cmd/c build.bat',在這種情況下,輸出可以從Java子程序中讀取,如果需要的話。

+0

對我來說,它說Windows無法找到「build.bat」。那麼我應該把這個文件放在哪裏?或者我應該如何給路徑。有什麼建議麼? – nanospeck 2014-07-14 08:38:31

+1

可以說我有一組命令,然後迭代該數組以執行所有命令 (i = 0到commands.length)Runtime.getRuntime()。exec(「cmd/c start buil.bat」) ; } 然後對於每次迭代(對於每個命令),命令窗口被打開,這是顯而易見的。如何避免我的意思是在一個窗口上執行所有的命令。 – viveksinghggits 2016-06-28 08:24:11

+0

我們有一些直接調用「gradlew.bat」的代碼,沒有在它前面放置「cmd/c」的東西,並且代碼在某種程度上工作。所以我猜想Java或Windows在某個時候解決了問題的一部分。如果我們嘗試執行「gradlew」,那麼雖然失敗了,但很明顯,「.bat」仍然需要結束。 – Trejkaz 2017-09-26 06:52:48

9

用於運行批處理腳本的可執行文件是cmd.exe它使用/c標誌指定批處理文件的名稱來運行:

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"}); 

理論上你也應該能夠以這種方式運行使用SCons,雖然我沒有測試過這個:

Runtime.getRuntime().exec(new String[]{"scons", "-Q", "implicit-deps-changed", "build\file_load_type", "export\file_load_type"}); 

編輯:阿馬拉,你說這是行不通的。您列出的錯誤是您從Windows機器上的Cygwin終端運行Java時遇到的錯誤;這是你在做什麼?問題在於Windows和Cygwin有不同的路徑,所以Windows的Java版本在你的Cygwin路徑上找不到scons可執行文件。如果事實證明這是你的問題,我可以進一步解釋。

+0

謝謝。它仍然不起作用 - 這段代碼甚至不會在我的應用程序中執行。我會嘗試你提供的其他選項。再次感謝。 – Amara 2009-03-05 18:34:42

+0

當我嘗試第二個替代它給了我這個錯誤:線程「主」異常java.io.IOException:無法運行程序「scons」:CreateProcess錯誤= 2,系統找不到指定的文件 – Amara 2009-03-05 18:37:13

+0

不,我不有Cygwin終端。我使用Windows命令終端。這很奇怪 - 我不知道爲什麼它不起作用。它完全讓我感到困惑。 – Amara 2009-03-05 18:54:24

18
Runtime runtime = Runtime.getRuntime(); 
try { 
    Process p1 = runtime.exec("cmd /c start D:\\temp\\a.bat"); 
    InputStream is = p1.getInputStream(); 
    int i = 0; 
    while((i = is.read()) != -1) { 
     System.out.print((char)i); 
    } 
} catch(IOException ioException) { 
    System.out.println(ioException.getMessage()); 
} 
20

有時線程執行過程中的時間比JVM線程在等待過程中的時間,用它來發生的,當你調用的過程需要一定的時間來進行處理,按如下方式使用WAITFOR()指令高:

try{  
    Process p = Runtime.getRuntime().exec("file location here, don't forget using/instead of \\ to make it interoperable"); 
    p.waitFor(); 

}catch(IOException ex){ 
    //Validate the case the file can't be accesed (not enought permissions) 

}catch(InterruptedException ex){ 
    //Validate the case the process is being stopped by some external situation  

} 

這樣JVM就會停止,直到proc在你繼續執行線程執行堆棧之前,你正在調用它。

13

運行使用Java的批處理文件,如果這是你在談論...

String path="cmd /c start d:\\sample\\sample.bat"; 
Runtime rn=Runtime.getRuntime(); 
Process pr=rn.exec(path);` 

這應該這樣做。

3
Process p = Runtime.getRuntime().exec( 
    new String[]{"cmd", "/C", "orgreg.bat"}, 
    null, 
    new File("D://TEST//home//libs//")); 

用jdk1.5和jdk1進行了測試。6

這對我來說工作得很好,希望它也能幫助別人。 爲了得到這個我掙扎了更多的日子。 :(

0

以下工作正常:

String path="cmd /c start d:\\sample\\sample.bat"; 
Runtime rn=Runtime.getRuntime(); 
Process pr=rn.exec(path); 
2

我有同樣的問題,但有時CMD運行失敗,我的文件 這就是爲什麼我創造我的桌面上temp.bat,未來這個溫度。 .bat將運行我的文件,然後臨時文件將被刪除

我知道這是一個更大的代碼,然而,即使在Runtime.getRuntime().exe()失敗

// creating a string for the Userprofile (either C:\Admin or whatever) 
String userprofile = System.getenv("USERPROFILE"); 

BufferedWriter writer = null; 
     try { 
      //create a temporary file 
      File logFile = new File(userprofile+"\\Desktop\\temp.bat"); 
      writer = new BufferedWriter(new FileWriter(logFile)); 

      // Here comes the lines for the batch file! 
      // First line is @echo off 
      // Next line is the directory of our file 
      // Then we open our file in that directory and exit the cmd 
      // To seperate each line, please use \r\n 
      writer.write("cd %ProgramFiles(x86)%\\SOME_FOLDER \r\nstart xyz.bat \r\nexit"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       // Close the writer regardless of what happens... 
       writer.close(); 
      } catch (Exception e) { 
      } 

     } 

     // running our temp.bat file 
     Runtime rt = Runtime.getRuntime(); 
     try { 

      Process pr = rt.exec("cmd /c start \"\" \""+userprofile+"\\Desktop\\temp.bat"); 
      pr.getOutputStream().close(); 
     } catch (IOException ex) { 
      Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); 

     } 
     // deleting our temp file 
     File databl = new File(userprofile+"\\Desktop\\temp.bat"); 
     databl.delete(); 
0

此代碼將執行路徑C:/ folders/folder中存在的兩個commands.bat。

Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");