0

I am developing a swing application in which I am running a cmd command using Runtime.getRuntime.execute(); and reading the output of command using BufferReader. Now what I want is I want to show the output in dialog with changing the text(Using JTextPane) inside it or appended strings in a textarea with the output of command like we can see while starting Eclipse of any other IDE for loading prerequisites.

I tried making subclass of JDialog class but it is not doing what i want.

My Code :

Process p = Runtime.getRuntime().exec(commandToBeExecuted);
            BufferedReader in = new BufferedReader(newInputStreamReader(p.getInputStream()));
            String line = null;
                  MessageDialog myDialog=new MessageDialog(UserInterface.this);

            while ((line = in.readLine()) != null) {
                  System.out.println(line);  
                  myDialog.setText(line);
                  myDialog.setVisible(true);
                  myDialog.setLocationRelativeTo(UserInterface.this);
            }
        } catch (Exception ex) {
            Logger.getLogger(UserInterface.class.getName()).log(Level.SEVERE, null, ex);
            errorGlobal.setText("Some Exception Occured in update "+ex.getMessage());
            System.out.println("Exception occured " + ex.getMessage());
        }   

Is it possible to do what I want and what will be the best way to achieve this ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • 1) Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 21 '13 at 08:29
  • You say it is "not doing what i want". What do you mean by that? – tbodt Jul 21 '13 at 08:31
  • Please have a look at this [example](https://www.dropbox.com/s/ocv7zhospy95map/SwingWorkerExample.java), it might can give you the desired idea. Just run the Program, click `Start` Button, then type anything on the console, it will be displayed on the `JTextArea`, if you want to stop performing reading from the `Console`, click `Stop` Button. Though it is not fully done with, its under progress now :-) – nIcE cOw Jul 21 '13 at 09:09
  • it is not changing the text the dialog is completely empty. Only the text text will be shown when execution completed. – Piyush Agarwal Jul 21 '13 at 09:15
  • @pyus13 : Click start button, then type on the console. What you type on console, will be added to the JTextArea. Moreover, you might be looking for is, this method [myDialog.setTitle(...)](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.html#setTitle(java.lang.String)) – nIcE cOw Jul 21 '13 at 09:41

1 Answers1

0

Your problem is in the loop. Use JTextArea.append and put the rest outside the loop,

tbodt
  • 16,609
  • 6
  • 58
  • 83