2017-10-13 108 views
0

所以我想執行一個命令,你可以在我的Java程序的cmd上執行命令。在做了一些研究之後,我想我找到了一個辦法來做到這一點。但是,我的代碼不起作用。如何運行你可以在Java終端上運行的命令

我的代碼是

import java.io.*; 

public class CmdTest { 
    public static void main(String[] args) throws Exception { 
     String[] command = {"ag","startTimes conf.js >> pro.txt"}; 
     ProcessBuilder builder = new ProcessBuilder(command); 
     builder.directory(new File("./test-java/")); 
     Process p = builder.start(); 
    } 
} 

程序執行,但不產生任何輸出。我嘗試使用其他命令,如「ls -a」,但仍然沒有輸出。

有人可以幫我調試這個或建議一個更好的方式做到這一點?謝謝

編輯1:我在Mac上執行此操作。如果這是必要的調試

編輯2:通常的LS和其他命令正在與你們提供的解決方案。然而,我想在Java程序中使用ag(the_silver_searcher)命令。當我嘗試,我得到以下錯誤 -

Exception in thread "main" java.io.IOException: Cannot run program "ag startTimes conf.js >> pro.txt": error=2, No such file or directory 
+0

你是在Windows還是Linux環境中運行它? –

+0

@AustinA,我在Mac上運行這個 – Sid

回答

0

現有答案爲您提供了有關如何解決代碼問題的信息,但他們沒有給出您的代碼無法工作的原因。

當你在一個shell上執行一個程序時,在程序執行之前,shell會完成相當多的處理。你的命令行

String[] command = {"ag","startTimes conf.js >> pro.txt"}; 
    ProcessBuilder builder = new ProcessBuilder(command); 

假設命令ag參數startTimes conf.js >> pro.txt運行 - 很可能不是你想要做什麼。讓我們更進一步:如果你寫了

String[] command = {"ag","startTimes", "conf.js", ">>", "pro.txt"}; 
    ProcessBuilder builder = new ProcessBuilder(command); 

這將假設ag命令知道的>>參數重定向輸出 - 這裏是其中殼發揮作用:該>>運算符是一個指令到外殼,告訴做什麼與過程的stdout輸出。當由shell啓動時,ag進程永遠不知道這個重定向,並且完全不知道>>和目標文件名。

有了這些信息,只需使用任何其他答案的代碼示例。我不會將它們複製到我的正確歸屬中。

+0

非常感謝你,奧拉夫,你的投入真的幫助。我終於能夠解決問題 – Sid

0

雖然有ProcessBuilder,我一直使用Runtime.getRuntime().exec("cmd");

Process Runtime.exec(String)

它返回一個Process,你可以得到的輸入和輸出流

即使您留在ProcessBuilder,您仍應該有權訪問Process.get<Input/Output/Error>Stream()

+0

我得到錯誤 - 「線程中的異常」main「java.io.IOException:無法運行程序」ag startTimes conf.js >> pro.txt「:error = 2,No這樣的文件或目錄「。你建議我做什麼? – Sid

+0

@Sid嘗試放入完整的文件路徑,它可能從本地目錄運行命令 – phflack

0

您需要通過從過程打開的輸入流讀取過程的輸出:

try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())) { 
    System.out.println(reader.readLine()); // process the output stream somehow 
} 

另外,你可能會在讀取錯誤流(p.getErrorStream()),我經常都在一個單獨的數據流進行,在Java 8中,您可以使用ProcessBuilder上的redirectErrorStream(true)自動將錯誤流添加到輸入流。當然,你不能區分輸入來自哪一個流,但它使閱讀更容易。如果您未讀取輸入或錯誤流,並且進程的緩衝區已滿,則進程會暫停,直到緩衝區中有足夠空間。

+0

我得到錯誤 - 「線程中的異常」main「java.io.IOException:無法運行程序」ag startTimes conf.js >> pro。 txt「:錯誤= 2,沒有這樣的文件或目錄」。你建議我做什麼? – Sid

0

也可以被重定向輸出到控制檯的啓動方法前加

builder.redirectOutput(ProcessBuilder.Redirect.INHERIT); 

+0

嘿,本,當我使用像ls這樣的命令時,你的解決方案就可以工作。但是當我使用ag命令時,出現錯誤 - 線程「main」異常java.io.IOException:無法運行程序「ag startTimes conf.js >> pro.txt」:error = 2,No such file或目錄 – Sid

+0

我現在可以做什麼? – Sid

+1

你在問一個新問題。接受提供給您的兩種解決方案中的一種並且可能發佈新的SO問題是公平的。 –

0
//"ls" command runs under the "sh" on linux(cmd.exe on windows), so first arg is "sh"  
//second arg "-c" tells "sh" which exact command should be executed 
//"ls" is actual command 
//"startTimes" as I understand is a file or directory, it is arg for "ls" command 
//"conf.js" is second arg for "ls" command 
new ProcessBuilder("sh", "-c", "ls", "startTimes", "conf.js") 
//set working dir for "sh" process" 
      .directory(new File("./test-java/")) 
//output will be written to "pro.txt" in working dir of "sh" process 
      .redirectOutput(new File("./test-java/pro.txt")) 
      .start(); 
+1

請回顧[我如何寫一個好的答案](https://stackoverflow.com/help/how-to-answer)。不接受代碼的答案是不鼓勵的,因爲他們沒有解釋他們如何解決問題中的問題。你應該更新你的答案,以解釋這是什麼以及如何解決問題。 – FluffyKitten