使java与C ++程序通信

时间:2015-01-29 06:10:29

标签: java c++ command-line io communication

我有一个C ++程序,它使用命令行作为IO的意思。我不懂C ++,也没有程序的源代码。我希望我的java应用程序打开C ++程序,提供一些输入并从C ++代码中收集结果。有办法吗?

更新:我需要在运行时输入输入。

1 个答案:

答案 0 :(得分:1)

您可以使用java.lang.Runtime

例如:

 public class TestRuntime {
        public static void main(String[] args) {
            try {
                Process p = Runtime.getRuntime().exec("test.bat");
                // test.bat or test.sh in linux is script with command to run (c++) program 
                //  or direct path to application's exec
                BufferedReader in = new BufferedReader(
                                    new InputStreamReader(p.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

此外,您可以在this topic中了解运行时和ProcessBuilder之间的区别。

相关问题