2017-09-25 81 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