2017-09-25 78 views
-1

我正在編寫一個Java程序來捕獲終端命令的輸出。在「正常」情況,即在那裏我直接執行命令到終端自己,我可以看到以下結果:Java運行時getInputStream忽略大多數終端命令輸出

enter image description here

然而,我的Java程序提供的輸出只抓住了一個小的子集,在這裏看到:

enter image description here

這是我講的基本代碼:

import java.io.*; 

class evmTest { 

    public static void main(String[] args) { 
     String evmResult = ""; 

     String evmCommand = "evm --debug --code 7f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000020101 run"; 
     try { 
      Runtime r = Runtime.getRuntime(); 
      Process p = r.exec(evmCommand); 

      BufferedReader in = new BufferedReader(new 
      InputStreamReader(p.getInputStream())); 
      String inputLine; 
      while ((inputLine = in.readLine()) != null) { 
       System.out.println(inputLine); 
       evmResult += inputLine; 
      } 
      in.close(); 

     } catch (IOException e) { 
      System.out.println(e); 
     } 

    } 
} 

到目前爲止,我還沒有能夠確定爲什麼該代碼只能發出微薄的0x。我已經發布了這個問題,希望有人能夠幫助我追蹤這個錯誤的原因。

回答

-1

做這樣的:

import java.io.*; 

class evmTest { 

public static void main(String[] args) { 
    String evmResult = ""; 

    String evmCommand = "evm --debug --code 7f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000020101 run"; 
    try { 

     Runtime rt = Runtime.getRuntime(); 
     String command = "evm --debug --code 7f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000020101 run"; 
     Process proc = rt.exec(command); 

     BufferedReader stdInput = new BufferedReader(new 
      InputStreamReader(proc.getInputStream())); 

     BufferedReader stdError = new BufferedReader(new 
      InputStreamReader(proc.getErrorStream())); 

     // read the output from the command 
     System.out.println("Here is the standard output of the command:\n"); 
     String s = null; 
     while ((s = stdInput.readLine()) != null) { 
      System.out.println(s); 
     } 

     // read any errors from the attempted command 
     System.out.println("Here is the standard error of the command (if any):\n"); 
     while ((s = stdError.readLine()) != null) { 
      System.out.println(s); 
     } 

    } catch (IOException e) { 
     System.out.println(e); 
    } 

} 

} 
+0

你需要在單獨的線程來讀取這些流,或者將它們合併。 – EJP