与 C++ 沟通来自 Java 的进程

发布于 2024-10-09 09:25:16 字数 706 浏览 1 评论 0原文

首先,我在网站上看到了一些关于此问题的问题,但没有看到任何解决我的问题的答案。

我有一个用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩心不撩汉 2024-10-16 09:25:16

如果你想从Java调用C和C++等本机应用程序,你需要使用JNI。

If you want to call native applications like C and C++ from Java, you need to use JNI.

简美 2024-10-16 09:25:16

我建议在程序启动时将输入放入程序中,它可能会在需要时使用它作为输入。

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.

醉态萌生 2024-10-16 09:25:16

以下是我在 Java 中执行任何命令行的方法。该命令行可以执行任何程序:

private String executionCommandLine(final String cmd) {

    StringBuilder returnContent = new StringBuilder();
    Process pr;
    try {
        Runtime rt = Runtime.getRuntime();
        pr = rt.exec(cmd);

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line = null;

        while ((line = input.readLine()) != null) {
            returnContent.append(line);
        }
        input.close();
        LOG.debug(returnContent.toString());

        // return the exit code
        pr.waitFor();
    } catch (IOException e) {
        LOG.error(e.getMessage());
        returnContent = new StringBuilder();
    } catch (InterruptedException e) {
        LOG.error(e.getMessage());
        returnContent = new StringBuilder();
    }

    return returnContent.toString();

}

Here is how I execute any command line in Java. This command line may execute any program:

private String executionCommandLine(final String cmd) {

    StringBuilder returnContent = new StringBuilder();
    Process pr;
    try {
        Runtime rt = Runtime.getRuntime();
        pr = rt.exec(cmd);

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line = null;

        while ((line = input.readLine()) != null) {
            returnContent.append(line);
        }
        input.close();
        LOG.debug(returnContent.toString());

        // return the exit code
        pr.waitFor();
    } catch (IOException e) {
        LOG.error(e.getMessage());
        returnContent = new StringBuilder();
    } catch (InterruptedException e) {
        LOG.error(e.getMessage());
        returnContent = new StringBuilder();
    }

    return returnContent.toString();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文