I am trying to run a command line program and interact with it, i.e., give commands as well as take commands in response and the commands I give should change the previous state of the program. I tried and have been able to execute it successfully but for the interaction part there is no response. This is my code :
protected void doWork() {
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
ProcessBuilder builder = new ProcessBuilder("e:/program files/urjtag/jtag.exe");
builder.redirectErrorStream(true);
Process process = builder.start();
ConsoleReader consoleReader = new ConsoleReader(process.getInputStream());
consoleReader.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s = "";
while((s=br.readLine())!=null){
System.out.println(s);
}
System.out.println(consoleReader.getResult()+" <<<<<<<<<<<<<");
int waitFor = process.waitFor();
consoleReader.join();
switch (waitFor) {
case 0:
return consoleReader.getResult();
default:
throw new RuntimeException("Failed to execute " + builder.command() + " \nReturned message: "
+ consoleReader.getResult());
}
}
@Override
protected void done() {
try {
showCommandResult(get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
showCommandError(e);
}
}
};
worker.execute();
}
I took it from here.
I took the idea of using BuffuredReader from here, but there is just no response from the program.
Also when I execute the program nothing shows up, though when I execute it in the folder it shows up its own prompt.