Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to direct console output to a textarea

827 views
Skip to first unread message

abhpr...@gmail.com

unread,
Jan 5, 2017, 2:56:15 AM1/5/17
to
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;
}
}

Nigel Wade

unread,
Jan 5, 2017, 5:35:53 AM1/5/17
to
On 05/01/17 07:56, abhpr...@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.

abhpr...@gmail.com

unread,
Jan 5, 2017, 6:04:06 AM1/5/17
to
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

Nigel Wade

unread,
Jan 9, 2017, 7:12:03 AM1/9/17
to
On 05/01/17 11:04, abhpr...@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.

Abhay Prakash

unread,
Jan 9, 2017, 10:17:05 AM1/9/17
to
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 :)

kanisfate...@gmail.com

unread,
Dec 18, 2018, 2:30:41 PM12/18/18
to
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?

kanisfate...@gmail.com

unread,
Dec 21, 2018, 3:35:13 AM12/21/18
to
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?
0 new messages