在java中打印c++ .exe文件的输出
Print output of c++ .exe file in java
我想从java运行c++ .exe文件,也想打印.exe文件的输出。我尝试并成功地从java运行c++ .exe文件,但我不知道如何使用java打印c++ .exe文件的输出(在java输出字段中),我尝试使用processExitValue和等待方法,但没有得到所需的输出。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");
}
如果你能帮我解决这个问题,我会很感激的。提前感谢
您可以使用Scanner
,然后从中读取行。您确定您的进程在标准输出上写入了一些内容吗?
read():从输入流中读取下一个字节的数据。
你必须使用扫描器:
Scanner scan = new Scanner(p.getInputStream());
String line = scan.getNextLine();
关于Deestan的回答:getInputStream()
在这里是正确的方法,我们想要过程的输出,这是应用程序的输入。
使用"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);
}
相关文章:
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- C++对象数组的文件输出
- bcdedit 文件输出不起作用,但其他命令可以
- 如何从文本文件输出数据?(学生成绩单计划)
- 使用 C++文件 I/O 输出 pdf 文件
- C 文件输出垃圾
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++:具有随机名称的文件输出
- 在命令提示符下看到换行符,但在文件输出中可以看到 相同的字符串
- 生成文件不会生成预期的文件输出
- C++11 fgetc 在"r+b"模式下使用时向我的文件输出 0
- 使用函数从文件输出
- GCC.obj文件输出不具有确定性(.debug_info,PROGPITS部分)
- C cstdiofile :: Writestring给出了不正确的文件输出
- C 输出:STD :: COUT和文件输出没有相同的内容
- GCC和MSVC之间的文件输出不一致
- C++文件输出奇怪的数字,并且部分代码没有运行
- 如何将特定字符从TXT文件输出到特定点
- C 调用从TXT文件输出信息的函数
- 将交易从文本文件输出到屏幕