2017-03-20 54 views

回答

0

嘗試通過每個參數單獨,而不是在一個字符串結合整個命令:

String[] cmd = {"./Hello","<","in.txt"}; 

Runtime rt = Runtime.getRuntime(); 
Process proc = rt.exec(cmd); 

希望這有助於;

0

我認爲這是,你想從執行的命令得到輸出。當調用Runtime.exec("<some command>")時,Java會設置獨立的IO-Stream來讀寫執行的命令。

如果你要打印的命令,命令行的結果,那麼你可以做這樣的事情:

public class Test { 


    public static void main(String[] args) throws IOException 
    { 
     int read; 
     byte[] buffer = new byte[1024]; 
     Process p = Runtime.getRuntime().exec("echo HELLO-THERE"); 
     InputStream is = p.getInputStream(); 
     while (is.available() > 0) { 
      read = is.read(buffer); 
      System.out.println(new String(buffer, 0, read, "UTF-8")); 
     } 
    } 
} 

這將產生這樣的:

HELLO-THERE 

一般來說,在情況像這樣,你最好閱讀java文檔:https://docs.oracle.com/javase/7/docs/api/

相關問題