如何确定进程是否等待用户输入

How to determine if the process waits input from the user

我的应用程序是获取 c++ CPP 文件并编译它,然后 运行 它。一切似乎都很完美。但是当用户忘记输入并且 c++ 程序需要输入时,程序停止工作。那么有没有办法知道 c++ 程序是否需要用户输入,这样我就可以处理用户忘记输入的情况?如果不能,我该如何处理这种情况?

我的 运行 函数代码是:

public void runFile(String input) {
    try {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("bash", "-c", "./a.out");
        Process process = processBuilder.start();

        BufferedOutputStream writer = new BufferedOutputStream(process.getOutputStream());
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        writer.write((input + "\n").getBytes());
        writer.flush();

        String s = null;
        // Read the output from the command:
        textArea2.setText(stdInput.readLine());
        while ((s = stdInput.readLine()) != null)
            textArea2.setText(textArea2.getText() + "\n" + s);
        
        // Read any errors from the attempted command:
        if((s = stdError.readLine()) != null) {
            textArea2.setText(s);
            while ((s = stdError.readLine()) != null) {
                textArea2.setText(textArea2.getText() + "\n" + s);
            }
        }

    } catch (Exception e) {
        textArea2.setText(e.toString());
        e.printStackTrace();
    }
}

经过大量搜索和阅读有关此错误的信息后,我发现无法知道进程是否在写入输出之前等待输入,也没有办法 Process class例如,当 c++ 程序需要两个输入而用户只输入一个输入时,阻止 java 程序被冻结并停止工作。

如何处理这种情况:我使用了Thread如下面的新代码来保持java程序在这种情况下保持存活并阻止它被冻结。

public byte runFile(String input, byte compilere, String fileName) {
    try {
        ProcessBuilder processBuilder = new ProcessBuilder();

        if (compilere == 0) // c++  code
            processBuilder.command("bash", "-c", "./a.out");
        else
            processBuilder.command("bash", "-c", "java " + fileName);

        Process process = processBuilder.start();

        BufferedOutputStream writer = new BufferedOutputStream(process.getOutputStream());
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        if (!input.isEmpty()) {
            writer.write((input + "\n").getBytes());
            writer.flush();
        } else {
            textArea2.setText("Input field is empty!");
            return 1;
        }

        // Read the output from the command:
        thread1 = new Thread() {
            public void run() {
                try {
                    String s = null;
                    s = stdInput.readLine();
                    if (s != null) {
                        textArea2.setText(s);
                        while ((s = stdInput.readLine()) != null)
                            textArea2.setText(textArea2.getText() + "\n" + s);
                    }
                    thread2.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        // Read any errors from the attempted command:
        thread2 = new Thread() {
            public void run() {
                try {
                    String s = null;
                    s = stdError.readLine();
                    if (s != null) {
                        textArea2.setText(s);
                        while ((s = stdError.readLine()) != null)
                            textArea2.setText(textArea2.getText() + "\n" + s);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        thread1.start();

        thread3 = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(8000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (textArea2.getText().isEmpty()) {
                    textArea2.setText("The input is not complete or the your program is slow !");
                }
            }
        };
        thread3.start();

    } catch (Exception e) {
        textArea2.setText(textArea2.getText() + "\n" + e.toString());
        e.printStackTrace();
        return 1;
    }
    return 0;
}

编辑:Marquis of Lorne 的评论后,我处理了没有输入且 c++ 应用程序需要输入的情况.