0

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.

9
  • Cannot see any Process.getOutputStream() and write to it Commented Jun 17, 2013 at 11:25
  • @Jayan sir, yes, there is none because I am stuck at getting InputStream successfully first. If you say sir I can add that too. Also I dont know how to use getOutputStream but that is not the reason why I didn't add it. Commented Jun 17, 2013 at 11:30
  • A BufferedReader will read a whole line, I doubt it will work if you have to deal with prompts Commented Jun 17, 2013 at 11:32
  • By the way, where does that ConsoleReader come from? Commented Jun 17, 2013 at 11:33
  • @fge the person says it worked here, though we still have to consider the fact that it is a third party program, so it might not be working there, but that's what the question is all about, what will work then? The ConsoleReader can be ignored for now sir, though I copied it from here Commented Jun 17, 2013 at 11:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.