从 Java 与 C++ 程序通信 [英] Communicating to a C++ program from java

查看:42
本文介绍了从 Java 与 C++ 程序通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 java 中执行外部 .exe 程序..exe 是一个 CLI 应用程序,它在运行时接受输入( scanf() )并根据输入输出.我可以使用

I want to execute a external .exe program from within java. The .exe is a CLI application which takes input in runtime( scanf() ) and outputs depending on the input. I can call the program to execute from java using

Process p = Runtime.getRuntime().exec("cmd /c start a.exe");

代替

Process p = Runtime.getRuntime().exec("cmd /c start a.exe");

但我认为也可以从java内部调用程序.我的整个程序都是用 C++ 编写的,只需要一个用 Java 编写的 GUI.有几点需要注意:=

But I think it is also possible to call a program from within java. I have my whole program written in C++ just need a GUI which is written in java. There are a few things to notice:=

1) 与 .exe 的通信应该是运行时(而不是通过 main(args) )2)java程序应该获取输出并存储在一些变量/面板中以供将来使用3) 要执行的程序可能不同(例如,用户可以选择一个根本不接受任何输入的 .exe)........所以基本上java GUI将充当RuntimeEnv

1) The communication with the .exe should be runtime (not through main(args) ) 2) The java program should take the outputs and store in some variable / panel to use for future 3) Program to be executed can differ ( for example user may select a .exe that doesnt take any input at all) ........So basically the java GUI will act as a RuntimeEnv

 public void runEXE() 

       {
            String s = null;

            try {
                Process p = Runtime.getRuntime().exec("cmd /c a.exe");
                System.exit(0);
            }
            catch (IOException e) {
                System.out.println("exception happened - here's what I know: ");
                e.printStackTrace();
                System.exit(-1);
            }

       }

我知道有很多关于这个话题的问题.但我找不到其中任何一个很有用.

I know there are a lot of questions about this topic out there. But i cant find any of them much useful.

推荐答案

解决方案可以在这里找到获取输出和传递输入的问题到java下运行的执行进程

The solution can be found here Problem getting output and passing input to a executing process running under java

这篇关于从 Java 与 C++ 程序通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆