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.

Community
  • 1
  • 1
Sukhmeet Singh
  • 29
  • 1
  • 11
  • Cannot see any Process.getOutputStream() and write to it – Jayan Jun 17 '13 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. – Sukhmeet Singh Jun 17 '13 at 11:30
  • A `BufferedReader` will read a _whole line_, I doubt it will work if you have to deal with prompts – fge Jun 17 '13 at 11:32
  • By the way, where does that `ConsoleReader` come from? – fge Jun 17 '13 at 11:33
  • @fge the person says it worked [here](http://stackoverflow.com/questions/10441047/executing-a-command-on-the-command-line-and-reading-the-console-output-in-java), 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](http://stackoverflow.com/questions/17059985/how-to-open-any-command-line-program-through-a-swing-gui-and-pass-commands-to-it/17060728#17060728) – Sukhmeet Singh Jun 17 '13 at 11:36
  • In the post you link, there is a newline at the end of the input ;) Which is why it works with a reader – fge Jun 17 '13 at 11:37
  • @fge that too is not working in my case, although thanks for notifying, I am such a jerk in case of observing things clearly visible ;) – Sukhmeet Singh Jun 17 '13 at 11:45
  • @ Sukhmeet Singh : What is the functionality of jtag.exe? It looks it gives a prompt (http://urjtag.svn.sourceforge.net/viewvc/urjtag/tags/URJTAG_0_10/web/htdocs/book/_jtag_commands.html#_basic_commands) and your need to write there to get some output. As fge noted, you may not able to use BufferedReader. Try to use read in that case. – Jayan Jun 18 '13 at 04:50
  • @Jayan sir that's right and that's what I mentioned in the question above. By using read you mean I should use `new BufferedReader(isr).read()` ? – Sukhmeet Singh Jun 18 '13 at 05:04

0 Answers0