【发布时间】:2011-01-07 19:12:04
【问题描述】:
我有一个用 C++ 编写并在 Windows 控制台上运行的可执行程序 (.exe) 我有一个 java swing 应用程序,所以我希望我的 java 应用程序能够交互 使用控制台应用程序(发送输入并获取输出)。 但是该怎么做呢?
【问题讨论】:
-
你有能力修改可执行文件吗?
我有一个用 C++ 编写并在 Windows 控制台上运行的可执行程序 (.exe) 我有一个 java swing 应用程序,所以我希望我的 java 应用程序能够交互 使用控制台应用程序(发送输入并获取输出)。 但是该怎么做呢?
【问题讨论】:
你可以这样做
// Create the proccess in JAVA
Process proc = Runtime.getRuntime().exec("Name of application");
// Receive outputs from another program inside Java by a stream
InputStream ips = proc.getInputStream();
// Using the stream to get the messages from another program
String output = "";
int c = 0;
while ((c = ips.read()) != -1){
output+= (char)c;
}
//Inputs messages into another program
OutputStream ops = proc.getOutputStream();
ops.write("an byte array");
【讨论】:
您可以从 Java 程序中启动 C++ 程序,该程序允许您写入其标准输入并读取其标准输出。检查Runtime 类。
【讨论】: