与 C++ 沟通来自 Java 的进程
首先,我在网站上看到了一些关于此问题的问题,但没有看到任何解决我的问题的答案。
我有一个用Java编写的程序,它调用一个用C++编写的cmd程序。 (这是一个假设,因为我没有实际的源代码)我知道C++程序的预期I/O,在cmd中它是两行输出,然后等待字符串输入。 我知道程序的第一条输出行是通过错误流,并且我正确接收它(这是预期的),但我没有在错误或输入流中得到第二行。 我尝试在第一行(错误行)之后立即写入程序,并且没有卡住,但没有响应。 我尝试对每个流使用 3 个不同的线程,但同样,第一行之后的输入/错误流中没有收到任何内容,并且程序没有响应通过输出流进行写入。
我的初始化程序是:
Process p = Runtime.getRuntime().exec("c:\\my_prog.exe");
BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
这是否可能,或者可能取决于 C++ 程序?
谢谢, 本亚明
First, I saw a few Q's about this issue in the site, but didn't see any answer that solve my problem.
I have a program written in Java and it calls a cmd program written in C++. (this is an assumption since I don't have the actual source) I know the expected I/O of the C++ program, in the cmd it is two lines of output and then it waits for string input.
I know that the first output line of the program is through error stream, and I receive it properly (this is expected), but I don't get the second line in error or input stream.
I tried to write to the program right after the first line ( the error line) and didn't got stuck, but there was no response.
I tried using 3 different threads, for each stream, but again, nothing was received in input/error stream after the first line, and the program didn't respond to writing through output stream.
My initializers are:
Process p = Runtime.getRuntime().exec("c:\\my_prog.exe");
BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
Is it possible at all or maybe it depends on the C++ program?
Thanks,
Binyamin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想从Java调用C和C++等本机应用程序,你需要使用JNI。
If you want to call native applications like C and C++ from Java, you need to use JNI.
我建议在程序启动时将输入放入程序中,它可能会在需要时使用它作为输入。
I would suggest to put the input in the program when it has started, it will propably use that as input when it wants it.
以下是我在 Java 中执行任何命令行的方法。该命令行可以执行任何程序:
Here is how I execute any command line in Java. This command line may execute any program: