• how to direct console output to a textarea

    From abhprk3926@gmail.com@21:1/5 to All on Wed Jan 4 23:56:14 2017
    i cant seem to figure a way to display console output to a textarea. here is what i have done till now. any help would be appreciated.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;

    class test extends JFrame
    {
    JTextArea content;
    JSplitPane pane;
    JMenuBar jmb = new JMenuBar();
    JMenu menu = new JMenu("Options");
    JMenuItem item = new JMenuItem("Compile") , item1 = new JMenuItem("Run") , item2 = new JMenuItem("Save");

    test()
    {
    try
    {
    PipedInputStream in = new PipedInputStream();
    PipedInputStream out = new PipedInputStream();
    System.setIn(in);
    System.setOut(new PrintStream(new PipedOutputStream(out) , true));
    PrintWriter writer = new PrintWriter(new PipedOutputStream(in),true);



    setTitle("Testing Window");
    setSize(700,700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    content = new JTextArea();
    pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,new JScrollPane(content),new JScrollPane(compiler(out,writer)));
    pane.setResizeWeight(0.8);
    add(pane);
    menu.add(item);
    menu.add(item1);
    menu.add(item2);
    jmb.add(menu);
    setJMenuBar(jmb);

    }catch(Exception e){}

    ActionListener listener = (ActionEvent ae) -> {
    try(FileWriter file = new FileWriter("hello.java");
    BufferedWriter bw = new BufferedWriter(file))
    {
    Scanner sc = new Scanner(content.getText());
    while( sc.hasNext())
    {
    bw.write(sc.nextLine());
    bw.newLine();
    }

    }catch(Exception e){e.printStackTrace();}
    };
    item2.addActionListener(listener);


    ActionListener listener1 = (ActionEvent ae)->{

    try
    {
    Process p = Runtime.getRuntime().exec("javac hello.java");
    Scanner sc = new Scanner(p.getInputStream());
    p.waitFor();

    }catch(Exception e){e.printStackTrace();}


    };
    item.addActionListener(listener1);

    ActionListener listener2 = (ActionEvent ae)->{
    try
    {
    Process p = Runtime.getRuntime().exec("java hello");
    p.waitFor();

    }catch(Exception e){e.printStackTrace();}
    };
    item1.addActionListener(listener2);

    setVisible(true);
    }

    public static void main(String args[])
    {
    SwingUtilities.invokeLater( ()->{new test();} );
    }

    public static JTextArea compiler(final InputStream out, final PrintWriter in) {
    final JTextArea area = new JTextArea();

    // handle "System.out"
    new SwingWorker<Void, String>() {
    protected Void doInBackground() throws Exception {
    Scanner s = new Scanner(out);
    while (s.hasNextLine()) publish(s.nextLine() + "\n");
    return null;
    }
    protected void process(ArrayList<String> chunks) {
    for (String line : chunks) area.append(line);
    }
    }.execute();

    // handle "System.in"
    area.addKeyListener(new KeyAdapter() {
    private StringBuffer line = new StringBuffer();
    @Override public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (c == KeyEvent.VK_ENTER) {
    in.println(line);
    line.setLength(0);
    } else if (c == KeyEvent.VK_BACK_SPACE) {
    line.setLength(line.length() - 1);
    } else if (!Character.isISOControl(c)) {
    line.append(e.getKeyChar());
    }
    }
    });

    return area;
    }
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Nigel Wade@21:1/5 to abhprk3926@gmail.com on Thu Jan 5 10:35:50 2017
    On 05/01/17 07:56, abhprk3926@gmail.com wrote:
    i cant seem to figure a way to display console output to a textarea. here is what i have done till now. any help would be appreciated.

    You are only reading the standard output of the GUI program itself. If you want to read the output from the processes you create using Runtime.getRuntime().exec
    then you need to capture the output of those processes and append it to the text area.

    You would probably be better off using ProcessBuilder rather than Runtime.getRuntime().exec.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From abhprk3926@gmail.com@21:1/5 to All on Thu Jan 5 03:04:05 2017
    Can You Guide Me On As To How Exactly Read The Output Of the Process.
    i cant seem to figure a way out to read from the output stream of the process

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Nigel Wade@21:1/5 to abhprk3926@gmail.com on Mon Jan 9 12:11:59 2017
    On 05/01/17 11:04, abhprk3926@gmail.com wrote:
    Can You Guide Me On As To How Exactly Read The Output Of the Process.
    i cant seem to figure a way out to read from the output stream of the process


    If you create a process using ProcessBuilder, ProcessBuilder.start returns a Process.
    From the Process you get the standard output stream from Process.getInputStream, and
    the standard error from Process.getErrorStream.

    However, if the process is likely to produce output on both stdout and stderr then you
    need to read both in parallel on different threads to avoid possible deadlock. If you are
    ok with reading merged stdout and stderr then you can use ProcessBuilder.redirectErrorStream(true)
    before invoking ProcessBuilder.start().

    Since each sub-process generates its own stdout/stderr you have to do this for each process you create
    in the same logical block which creates the sub-process. The code which reads stdout/stderr must be run
    by a different thread from the one which waits for the process to complete, otherwise you risk deadlock.

    Therefore you'll need to modify your code so that the blocks which create your sub-processes also read the output
    from those processes, and since you want that output to be appended to a JComponent the output needs to be processed
    on the EDT. You already have a model for the code which might do this using SwingWorker, but you need to figure out
    how to get the Processes InputStream into the SwingWorker doInBackground method. Also, since it's an InputStream the easiest
    way to read it is to wrap it in an InputStreamReader, which in turn is wrapped by a BufferedReader. Then you can read
    using

    BufferedReader out = new BufferedReader(new InputStreamReader( process.getInputStream());

    while((line = out.readLine()) != null) {
    publish(line);
    }

    Another thing which you need to correct is your ActionListeners which create the sub-processes. At the moment they will block the
    EDT until the process is complete thus preventing the SwingWorkers from executing, and locking the GUI.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Abhay Prakash@21:1/5 to All on Mon Jan 9 07:17:02 2017
    Thanks Nigel . That guide of reading the stdout/stderr on different thread was a nice one sir as i didnt knew this before. thumbs up for that. i modified my code as you said, and it works now. thanks for the guidance :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From kanisfatemashanta28@gmail.com@21:1/5 to Abhay Prakash on Tue Dec 18 11:30:39 2018
    On Monday, 9 January 2017 21:17:05 UTC+6, Abhay Prakash wrote:
    Thanks Nigel . That guide of reading the stdout/stderr on different thread was a nice one sir as i didnt knew this before. thumbs up for that. i modified my code as you said, and it works now. thanks for the guidance :)

    then,can you publish final code after modification?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From kanisfatemashanta28@gmail.com@21:1/5 to Abhay Prakash on Fri Dec 21 00:35:11 2018
    On Thursday, 5 January 2017 17:04:06 UTC+6, Abhay Prakash wrote:
    Can You Guide Me On As To How Exactly Read The Output Of the Process.
    i cant seem to figure a way out to read from the output stream of the process

    I face same problem.now what can I do?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)