I know this has been asked before, but, as it happens to people sometimes, other answers didn't seem to help.
I need to start a C application and pass it a few inputs to navigate through its menu to finally execute what I need. The final output (result) is sent to a file, but the intermediate outputs (menu and sub-menus printed on the console) would be nice to have printed on my Eclipse console for debugging.
I wrote the following code based on what the user Vince posted on his question and later mentioned was working, but it doesn't seem to be doing it for me.
public final class InteractWithExternalApp {
private static PrintWriter printOut;
private static BufferedReader retrieveOutput;
private static Process p;
private EvaluationTests(){} // suppressing the class constructor
public static void Evaluate(String paramToApp) {
try
{
Runtime rt = Runtime.getRuntime() ;
p = rt.exec("C:\\Path\\To\\Desktop\\appName " + paramToApp);
InputStream in = p.getInputStream() ;
OutputStream out = p.getOutputStream ();
retrieveOutput = new BufferedReader(new InputStreamReader(in));
printOut = new PrintWriter(out);
// print menu
if((line = retrieveOutput.readLine()) != null) {
System.out.println(line);
}
// send the input choice -> 0
printOut.println("0");
printOut.flush();
// print sub-menu
if((line = retrieveOutput.readLine()) != null) {
System.out.println(line);
}
// send the input choice
printOut.println("A string");
printOut.flush();
// print sub-menu
if((line = retrieveOutput.readLine()) != null) {
System.out.println(line);
}
/*
Repeat this a few more times for all sub-menu options until
the app finally executes what's needed
*/
}catch(Exception exc){
System.out.println("Err " + exc.getMessage());
}
return;
}
Also, as an exercise, I tried opening Windows Command Prompt and sending it a command, following the example given here. cmd.exe opened fine, but then passing an echo
command didn't do anything.
OutputStream stdin = p.getOutputStream();
InputStream stdout = p.getInputStream();
stdin.write(new String("echo test").getBytes());
stdin.flush();
Can anybody please land a hand? Where am I going wrong?