在java中打印c ++ .exe文件的输出

时间:2013-05-27 13:25:12

标签: java c++

我想从java运行c ++ .exe文件,也想打印.exe文件的输出。试过并成功从java运行c ++ .exe文件,但是我没有得到如何打印输出(在java输出字段中)使用java的c ++ .exe文件,我尝试使用processExitValue和waitfor方法,但没有获得所需的输出.java代码在这里

int processExitVal = 0;
         try {
        Process p = Runtime.getRuntime().exec("cmd /c  start rs.exe");
        processExitVal = p.waitFor();
       // p.getOutputStream();
        //InputStreamReader ir=new InputStreamReader(p.getInputStream());
       //BufferedReader t = new BufferedReader((new InputStreamReader(p.getInputStream())));
       // int y=Integer.parseInt(t.readLine());
        InputStream in=p.getInputStream();
        System.out.println(in.read());
        //System.out.println("output"+Process.getOutputStream());
        } catch (IOException e) {
        System.out.println("IOException");
        e.printStackTrace();
        } catch (InterruptedException e) {
        System.out.println("InterruptedException");
        e.printStackTrace();
       }
       System.out.println(processExitVal);
       System.out.println("Execution complete");
}

如果你能解决这个问题,我将感激不尽。提前致谢

2 个答案:

答案 0 :(得分:0)

您可以使用Scanner然后从中读取行。你确定你的过程是在标准输出上写一些东西吗?

编辑:

  

read():从输入流中读取下一个数据字节。

您必须使用扫描仪:

Scanner scan = new Scanner(p.getInputStream());
String line = scan.getNextLine();

关于Deestan的答案:getInputStream()是正确的方法,我们需要流程的输出,这是应用程序的输入。

答案 1 :(得分:0)

使用“rs.exe”代替“cmd / c start rs.exe”

例如:

Process process = Runtime.getRuntime().exec("rs.exe");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = in.readLine()) != null) {
  System.out.println(line);
}