i have an executable program (.exe) writen in c++ and run on windows console and i have a java swing applecation , so i want my java application to interact with the console app (send input and get output) . but how to do that ?
Asked
Active
Viewed 5,285 times
1
-
Do you have the ability to modify the executable? – Michael Krauklis Jan 21 '10 at 18:24
2 Answers
3
You can do it this way
// 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");
marionmaiden
- 3,040
- 7
- 30
- 48
2
You can launch the C++ program from within the Java program which allows you to write to its standard input, and read its standard output. Check the Runtime class.
Bozho
- 572,413
- 138
- 1,043
- 1,132
Thorbjørn Ravn Andersen
- 71,889
- 31
- 184
- 335