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

dly7yett  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(399)

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

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();
    }
}
smtd7mpg

smtd7mpg1#

在对这个bug进行了大量搜索和阅读之后,我发现没有办法知道进程是否在写入输出之前等待输入,也没有办法进入 Process 类来停止java程序冻结和停止工作,例如,当c++程序需要两个输入,而用户只输入一个输入时。
如何处理这种情况:我曾经 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;
}

编辑:在洛恩侯爵的评论之后,我处理了没有输入而c++应用程序需要输入的情况。

相关问题