Re: Capturing both stdout and stderr of exec-ed process in JTextArea



Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx> sez:

Frank D. Greco wrote:
Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx> sez:

Frank D. Greco wrote:

Is posting code here ok? I narrowed the problem to a short-ish
hello-world app. I don't want to ruffle the e-feathers here. ;)

Actually, the real code or an SSCCE is a real help in diagnoses. So
post the code :-).

Ok. Here's my hello-world that shows the problem. There are three
classes here. The third one is trivial (it just sends stuff to stdout/stderr).
The first class is the driver; its responsible for setting up the Process and
execing. The 2nd class is what confuses me.

Thanks... f

To run the working code:
% java JtaCommandRunner java Dummy 100
To run the non-working code:
% java -Dinvokelater JtaCommandRunner java Dummy 100


# - JtaCommandRunner.java -------------------------------------
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class JtaCommandRunner {
private static JTextArea jta;
private static int returnValue = Integer.MIN_VALUE;


// ******************************************************
public JtaCommandRunner(JTextArea jta) {
this.jta = jta;
}


// ******************************************************
public void run(String[] args) {

try {

Process pro = null;

if (args.length > 1) {
pro = Runtime.getRuntime().exec(args);
} else {
pro = Runtime.getRuntime().exec(args[0]);
}

InputStream error = pro.getErrorStream();
InputStream output = pro.getInputStream();

Thread err = new Thread(new JtaOutReader(jta, error));
Thread out = new Thread(new JtaOutReader(jta, output));
out.start();
err.start();

returnValue = pro.waitFor();

} catch (java.io.IOException e) {
e.printStackTrace();

} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}

}

// ******************************************************

public static void main(String[] args) {

if (args.length < 1) {
System.out.println("Usage: java CommandRunner <command string>");
System.exit(-1);
}

JFrame f = new JFrame("CommandRunner");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());

JScrollPane jsp = new JScrollPane( jta = new JTextArea(50, 50) );
jta.setFont(new Font("Lucida Sans Typewriter", Font.PLAIN, 12));
f.add(jsp, BorderLayout.CENTER);

f.pack();
f.setVisible(true);

new JtaCommandRunner(jta).run(args);
}

}

# - JtaOutReader.java -------------------------------------
import javax.swing.*;
import java.io.*;

public class JtaOutReader implements Runnable {
private JTextArea jta;
private InputStream is;
private String temp = null;

public JtaOutReader(JTextArea jta, InputStream is) {
this.is = is;
this.jta = jta;
}

public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(is));

if ( System.getProperty("invokelater") == null ) {

// This works
while ((temp = in.readLine()) != null) {
jta.append(temp+"\n");
}

} else {

// This doesn't
while ((temp = in.readLine()) != null) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jta.append(temp+"\n");
}
});
}
}
is.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}
# - Dummy.java (just sends stuff to stdout/stderr)-------------------------

public class Dummy {
public static void main(String[] argv) {
int count = argv.length == 0 ? 50 : Integer.parseInt(argv[0]);

for (int i = 0; i < count; i++) {
System.out.println("STDOUT: " + i);
System.err.println(" STDERR: " + i);
}
}
}
.



Relevant Pages

  • Re: Accessing at runtime nested methods parameters values
    ... private object val; ... public override string ToString() ... public class Call ... public void PushName ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Capturing both stdout and stderr of exec-ed process in JTextArea
    ... private static int returnValue = Integer.MIN_VALUE; ... public JtaCommandRunner(JTextArea jta) { ... public void run{ ... Second, as Lew alluded in his post, on the one running on the EDT, the time between when the object is assigned to temp and when it is written to the JTextArea, the variable temp is getting reassigned. ...
    (comp.lang.java.gui)
  • Re: Problem mit BindingSource
    ... Public Class Form36 ... Private Sub Form36_Load(ByVal sender As System.Object, ... private MeinDTO _backup; ... public void BeginEdit() ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)
  • Re: Can we over-load "+" in Java?
    ... public class Person { ... private String sName; ... private Person leader; ... public void setTeamLeader ...
    (comp.lang.java.programmer)
  • Re: Can we over-load "+" in Java?
    ... public class Person { ... private String sName; ... private Person leader; ... public void setTeamLeader ...
    (comp.lang.java.programmer)