Menu

Write stdout to a text panel

Help
David
2004-12-23
2023-05-19
  • David

    David - 2004-12-23

    Dear all,

    I've incorporated j2ssh into a swing front end that will be used to kick off unix shell scripts.  I'd like to be able to capture the output from the scripts and write it to a jtextarea instead of stdout.

    What is a sensible way to approach this?

    Cheers!

     
  • alexpatri

    alexpatri - 2023-04-11

    One approach to capturing the output from the shell scripts and writing it to a JTextArea is to use a separate thread to execute the shell script and capture the output.

    You can create a new thread that uses the ProcessBuilder class to execute the shell script. You can redirect the output of the shell script to a temporary file and then read the file contents and append it to the JTextArea. Source LMT

    Here's an example code snippet:

    public class ScriptRunner implements Runnable {
        private JTextArea outputArea;
        private String command;
    
        public ScriptRunner(JTextArea outputArea, String command) {
            this.outputArea = outputArea;
            this.command = command;
        }
    
        public void run() {
            try {
                ProcessBuilder pb = new ProcessBuilder(command);
                pb.redirectErrorStream(true);
                Process p = pb.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    outputArea.append(line + "\n");
                }
                p.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    To use this class, you can create a new instance of ScriptRunner with the JTextArea and shell script command as arguments, and then execute it in a new thread:

    ScriptRunner runner = new ScriptRunner(outputArea, "/path/to/shell/script.sh");
    Thread t = new Thread(runner);
    t.start();
    

    Note that you should replace "/path/to/shell/script.sh" with the actual path to your shell script.

     
  • janesamth

    janesamth - 2023-05-19

    A sensible way to capture the output from shell scripts and write it to a JTextArea in a Swing application using j2ssh would be to use PipedInputStream and PipedOutputStream. Here's a general approach:

    1. Create an instance of PipedInputStream and PipedOutputStream:

    PipedInputStream pipedInput = new PipedInputStream();
    PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput);

    2. Create a separate thread to read the output from the PipedInputStream and update the JTextArea:

    Thread outputThread = new Thread(() -> {
    try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(pipedInput));
    String line;
    while ((line = reader.readLine()) != null) {
    SwingUtilities.invokeLater(() -> textArea.append(line + "\n"));
    }
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    });
    outputThread.start();

    3. Use PipedOutputStream to redirect the output from the executed shell script to the PipedInputStream. Assuming you have a Session object in j2ssh:

    Session session = // obtain the Session object
    SessionChannelClient channel = session.openSessionChannel();
    channel.setInputStream(null);
    channel.setOutputStream(pipedOutput);
    channel.executeCommand("your_shell_script.sh");

    Make sure to update textArea with the actual reference to your JTextArea component. This approach allows you to capture the output from the shell script execution and display it in real-time within the JTextArea of your Swing application

     

    Last edit: janesamth 2023-05-24

Log in to post a comment.